0
0
Wordpressframework~10 mins

Plugin security (nonces, sanitization) in Wordpress - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Plugin security (nonces, sanitization)
User submits form
Check nonce validity
Sanitize input
Process data
Save or output safe data
This flow shows how WordPress plugins use nonces to verify requests and sanitize inputs to keep data safe before processing.
Execution Sample
Wordpress
<?php
if (isset($_POST['submit'])) {
  if (isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'save_data')) {
    $safe_input = sanitize_text_field($_POST['user_input']);
    update_option('my_option', $safe_input);
  }
}
?>
This code checks a nonce from a form submission, sanitizes the user input, and saves it safely.
Execution Table
StepActionInput/Condition_ResultNext Step
1Form submittedPOST with _wpnonce and user_inputData receivedCheck nonce
2Verify nonce_wpnonce matches 'save_data'Nonce validSanitize input
3Sanitize inputRaw user_inputCleaned user_inputSave data
4Save dataCleaned user_inputOption updated safelyEnd
5If nonce invalid_wpnonce does not matchReject requestShow error message
💡 Execution stops after saving data or rejecting invalid nonce.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
_POSTemptycontains _wpnonce and user_inputsamesamesame
nonce_checkundefinedundefinedtrue or falsetrue or falsetrue or false
user_inputundefinedraw inputraw inputsanitized inputsanitized input
saved_optionold valueold valueold valueold valueupdated with sanitized input
Key Moments - 3 Insights
Why do we check the nonce before sanitizing input?
Checking the nonce first (see Step 2 in execution_table) ensures the request is legitimate before processing any data, preventing unauthorized actions.
What happens if the nonce is invalid?
If the nonce check fails (Step 5), the plugin rejects the request and does not sanitize or save data, protecting against CSRF attacks.
Why sanitize input even after nonce verification?
Nonce verification confirms request origin but does not clean data. Sanitization (Step 3) removes harmful content to prevent security issues like XSS.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at Step 3?
AThe user input is sanitized to remove unsafe characters.
BThe nonce is verified to check request validity.
CThe data is saved to the database.
DThe request is rejected due to invalid nonce.
💡 Hint
Refer to the 'Action' and 'Result' columns in Step 3 of the execution_table.
At which step does the plugin reject the request if the nonce is invalid?
AStep 2
BStep 3
CStep 5
DStep 4
💡 Hint
Check the 'Action' and 'Result' columns for rejection in the execution_table.
If the user input is not sanitized, what risk increases?
AThe nonce check will fail.
BThe plugin might save unsafe data causing security issues.
CThe form submission will be rejected.
DThe plugin will automatically sanitize later.
💡 Hint
Look at the purpose of sanitization in the key_moments section.
Concept Snapshot
Plugin Security with Nonces and Sanitization:
- Use wp_verify_nonce() to check request authenticity.
- Always sanitize user inputs with functions like sanitize_text_field().
- Reject requests if nonce is invalid to prevent CSRF.
- Sanitization protects against harmful data like scripts.
- Process and save only verified and sanitized data.
Full Transcript
This lesson shows how WordPress plugins keep data safe by using nonces and sanitization. When a user submits a form, the plugin first checks the nonce to confirm the request is genuine. If the nonce is valid, it then cleans the user input to remove any harmful content. Only after these checks does the plugin save the data. If the nonce is invalid, the plugin rejects the request to stop unauthorized actions. Sanitization is important even after nonce checks because it protects against unsafe data that could cause security problems. This step-by-step flow helps beginners understand how to secure plugin data handling.