Exercise 1: Why the Nonce Check Runs Before Sanitizing Input — Possible Solution ==================================================================== WHAT THE NONCE CHECK ACTUALLY VERIFIES ------------------------------ Per Chapter 8, a nonce confirms "a request genuinely originated from the expected page and user" - it's a check about WHETHER this request should be trusted and processed at all, not about the specific content of the data being submitted. WHY THIS HAS TO HAPPEN FIRST, LOGICALLY ------------------------------ If the nonce check fails, the request is not legitimate - it may be a forged, cross-site request that never should have reached this code handling real form data in the first place. Running sanitize_text_field() or any other processing on the submitted data BEFORE confirming the request is legitimate would mean doing real work (and potentially producing real side effects, like the eventual wp_mail() call) on data that might not have come from a trustworthy source at all. Checking the nonce first means an illegitimate request is rejected immediately, before any of its data is trusted or acted upon in any way. WHY THIS MATCHES CHAPTER 8'S OWN GUIDANCE ------------------------------ Per Chapter 8, "a genuinely secure privileged action checks both the nonce... and current_user_can()... neither one alone is a complete defense." While this specific form doesn't require a capability check (since it's meant to be usable by any visitor, including anonymous ones, given the admin_post_nopriv_contact_form hook), the same underlying principle applies to the nonce: security checks establishing whether a request should be trusted at all belong at the very start of the handler, before any processing of the request's actual content begins. WHY SANITIZING FIRST WOULD BE THE WRONG ORDER ------------------------------ Sanitizing the input first and checking the nonce afterward would mean the code has already done work on the submitted data (running it through sanitize_text_field()/sanitize_textarea_field()) even for a request that turns out to be illegitimate - wasted, and philosophically backwards, since the entire point of the nonce check is to gate whether this request should be processed at all before any processing happens. WHY THIS WORKS AS AN ANSWER ------------------------------ It explains precisely what the nonce check verifies, why gating request legitimacy has to logically come before any data processing, and connects the ordering directly to Chapter 8's own layered-security principle rather than treating the order as arbitrary.