Plugin security helps keep your WordPress site safe from bad actions. Nonces and sanitization stop hackers from doing harmful things.
Plugin security (nonces, sanitization) in Wordpress
<?php // Create a nonce field in a form wp_nonce_field('action_name', 'nonce_name'); // Check nonce on form submission if (!isset($_POST['nonce_name']) || !wp_verify_nonce($_POST['nonce_name'], 'action_name')) { die('Security check failed'); } // Sanitize text input $clean_text = sanitize_text_field($_POST['user_input']); // Sanitize URL input $clean_url = esc_url_raw($_POST['user_url']);
Nonces are special tokens to verify requests are safe.
Sanitization cleans user input to remove harmful code.
<?php // Add nonce field to form wp_nonce_field('save_settings', 'my_nonce');
<?php // Verify nonce when form is submitted if (!isset($_POST['my_nonce']) || !wp_verify_nonce($_POST['my_nonce'], 'save_settings')) { die('Invalid request'); }
<?php // Sanitize a text input $name = sanitize_text_field($_POST['name']);
<?php // Sanitize a URL input $url = esc_url_raw($_POST['website']);
This plugin creates a simple form with a nonce for security. It checks the nonce when the form is submitted and sanitizes the user input before showing it back safely.
<?php /* Plugin Name: Simple Secure Form */ // Show form with nonce function ssf_show_form() { echo '<form method="POST">'; wp_nonce_field('ssf_action', 'ssf_nonce'); echo '<label for="ssf_name">Name:</label>'; echo '<input type="text" id="ssf_name" name="ssf_name">'; echo '<input type="submit" value="Submit">'; echo '</form>'; } // Handle form submission function ssf_handle_form() { if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!isset($_POST['ssf_nonce']) || !wp_verify_nonce($_POST['ssf_nonce'], 'ssf_action')) { echo 'Security check failed.'; return; } $name = sanitize_text_field($_POST['ssf_name'] ?? ''); echo 'Hello, ' . esc_html($name) . '! Your form was submitted safely.'; } } // Hook to display form and handle submission add_shortcode('ssf_form', function() { ob_start(); ssf_handle_form(); ssf_show_form(); return ob_get_clean(); });
Always check nonces before processing form data to stop attacks.
Sanitize all user inputs before saving or displaying them.
Use esc_html() or similar functions when showing user data to avoid code running in the browser.
Nonces protect your plugin from fake requests.
Sanitization cleans user input to keep your site safe.
Always use both together when handling user data in plugins.
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
