What if a simple missing check lets hackers take over your WordPress site?
Why Plugin security (nonces, sanitization) in Wordpress? - Purpose & Use Cases
Imagine you build a WordPress plugin that accepts user input and performs actions without checking if the request is genuine or cleaning the input.
Hackers can trick your plugin to run harmful commands or inject bad code.
Without security checks, your plugin is open to attacks like cross-site request forgery (CSRF) and cross-site scripting (XSS).
This can lead to data loss, site crashes, or stolen information.
Using nonces ensures requests come from trusted sources, preventing unauthorized actions.
Sanitization cleans user input to block harmful code from running.
Together, they keep your plugin safe and your site secure.
$_POST['data'] // directly used without checkscheck_admin_referer('action_name'); $safe_data = sanitize_text_field($_POST['data']);
Secure plugins that protect users and data from common web attacks.
A contact form plugin that uses nonces to verify submissions and sanitizes inputs to prevent malicious scripts from running.
Manual input handling risks security breaches.
Nonces verify request authenticity.
Sanitization cleans input to block harmful code.
Practice
Solution
Step 1: Understand what a nonce is
A nonce is a unique token used to verify that a request comes from a trusted source.Step 2: Identify nonce purpose in plugins
Nonces prevent attackers from submitting fake or unauthorized requests to your plugin.Final Answer:
To protect against unauthorized or fake form submissions -> Option BQuick Check:
Nonce = Security token for requests [OK]
- Thinking nonce speeds up plugin
- Using nonce for styling
- Confusing nonce with data storage
Solution
Step 1: Recall the WordPress function for nonce verification
The correct function to verify a nonce iswp_verify_nonce().Step 2: Check the correct usage
The function takes the nonce value and the action name as parameters, likewp_verify_nonce($_POST['my_nonce'], 'my_action').Final Answer:
wp_verify_nonce($_POST['my_nonce'], 'my_action') -> Option CQuick Check:
Verify nonce = wp_verify_nonce() [OK]
- Using non-existent functions like check_nonce
- Passing wrong parameters order
- Not using $_POST or $_GET to get nonce
if ( ! wp_verify_nonce($_POST['nonce'], 'save_data') ) {
echo 'Invalid nonce';
} else {
$name = sanitize_text_field($_POST['name']);
echo 'Hello, ' . $name;
}Assuming the nonce is valid and
$_POST['name'] = '<script>alert(1)</script>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 DQuick Check:
Sanitize removes tags but keeps text inside [OK]
- Assuming sanitize_text_field allows HTML
- Ignoring nonce check result
- Confusing output with raw input
if ( ! wp_verify_nonce($_POST['nonce'], 'save_data') ) {
wp_die('Security check failed');
}
$name = sanitize_text_field($_POST['name']);
update_option('user_name', $name);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 AQuick Check:
Always check input exists before sanitizing [OK]
- Assuming update_option is wrong here
- Ignoring missing input checks
- Thinking nonce check is missing
// 1. Verify nonce
if ( ! wp_verify_nonce($_POST['nonce'], 'save_email') ) {
wp_die('Invalid request');
}
// 2. Sanitize input
$email = ???
// 3. Save email
update_option('user_email', $email);Choose the correct code for step 2:
Solution
Step 1: Confirm nonce verification
The nonce is verified correctly before processing input.Step 2: Choose proper sanitization for email
For emails,sanitize_email()is the correct function to clean and validate email format.Step 3: Understand other options
sanitize_text_field()is for plain text,esc_html()is for output escaping, andwp_strip_all_tags()removes tags but does not validate email.Final Answer:
$email = sanitize_email($_POST['email']); -> Option AQuick Check:
Email input = sanitize_email() [OK]
- Using sanitize_text_field for emails
- Confusing escaping with sanitizing
- Not verifying nonce before saving
Ready to prove it?
Pass the test and this pattern moves unknown
