Assuming the nonce is valid and $_POST['name'] = '<script>alert(1)</script>John'?
medium
A. Hello, John
B. Hello, John
C. Invalid nonce
D. Hello, alert(1)John
Solution
Step 1: Check nonce validity
The nonce is valid, so the else block runs.
Step 2: Understand sanitize_text_field effect
This function removes HTML tags like <script>, so the input becomes 'alert(1)John' because sanitize_text_field removes tags but leaves the text inside.
Step 3: Clarify output
The output will be 'Hello, alert(1)John' because the script tags are stripped but the text 'alert(1)' remains.
Final Answer:
Hello, alert(1)John -> Option D
Quick Check:
Sanitize removes tags but keeps text inside [OK]
Hint: sanitize_text_field strips HTML tags but keeps inner text [OK]
Common Mistakes:
Assuming sanitize_text_field allows HTML
Ignoring nonce check result
Confusing output with raw input
4. Identify the error in this WordPress plugin code snippet:
A. Not checking if $_POST['name'] is set before sanitizing
B. Missing nonce field in the form
C. Using update_option instead of add_option
D. No error, code is correct
Solution
Step 1: Check nonce verification
The nonce is verified correctly with wp_verify_nonce and wp_die on failure.
Step 2: Check input sanitization safety
The code sanitizes $_POST['name'] without checking if it exists, which can cause a PHP notice or error.
Final Answer:
Not checking if $_POST['name'] is set before sanitizing -> Option A
Quick Check:
Always check input exists before sanitizing [OK]
Hint: Check if input exists before sanitize_text_field [OK]
Common Mistakes:
Assuming update_option is wrong here
Ignoring missing input checks
Thinking nonce check is missing
5. You want to securely save a user email from a form in your WordPress plugin. Which combination of code snippets correctly uses nonce verification and sanitization?