📖 Scenario: You are building a WordPress plugin that needs to securely handle form submissions. To protect against unauthorized requests, you will use WordPress nonces (numbers used once) to verify that the form submission is valid and comes from your site.
🎯 Goal: Build a simple WordPress plugin that creates a form with a nonce field, and then verifies the nonce when the form is submitted to ensure the request is safe.
📋 What You'll Learn
Create a nonce field with a specific action name
Add a form that submits data via POST
Verify the nonce on form submission using the same action name
Display a success or error message based on nonce verification
💡 Why This Matters
🌍 Real World
Nonce verification is essential in WordPress plugins and themes to protect forms and actions from malicious attacks like CSRF (Cross-Site Request Forgery).
💼 Career
Understanding nonce verification is important for WordPress developers to build secure plugins and themes that protect user data and site integrity.
Progress0 / 4 steps
1
Create a nonce field in the form
Create a function called myplugin_form that outputs a simple HTML form with method post. Inside the form, add a nonce field using wp_nonce_field with the action name myplugin_nonce_action and the nonce name myplugin_nonce.
Wordpress
Hint
Use wp_nonce_field inside your form to create the nonce field with the exact action and name.
2
Add a form submission handler
Create a function called myplugin_handle_form that will process the form submission. Inside it, create a variable $nonce that gets the value from $_POST['myplugin_nonce']. This will be used to verify the nonce later.
Wordpress
Hint
Use the null coalescing operator ?? to safely get the nonce from $_POST.
3
Verify the nonce in the form handler
Inside the myplugin_handle_form function, use wp_verify_nonce to check if $nonce is valid for the action myplugin_nonce_action. If the nonce is valid, set a variable $message to 'Nonce verified successfully.'. Otherwise, set $message to 'Nonce verification failed.'.
Wordpress
Hint
Use wp_verify_nonce with the nonce and the exact action name to check validity.
4
Display the form and message
Call myplugin_handle_form to process the form submission. Then call myplugin_form to display the form. After the form, if the variable $message is set, echo it inside a <p> tag.
Wordpress
Hint
Call the handler first to get the message, then display the form, then show the message safely.
Practice
(1/5)
1. What is the main purpose of using a nonce in WordPress?
easy
A. To create database backups automatically
B. To speed up page loading times
C. To store user preferences
D. To protect against unauthorized actions and CSRF attacks
Solution
Step 1: Understand what a nonce is
A nonce is a security token used to verify requests are intentional and authorized.
Step 2: Identify nonce purpose in WordPress
WordPress uses nonces to protect against CSRF (Cross-Site Request Forgery) attacks by verifying actions.
Final Answer:
To protect against unauthorized actions and CSRF attacks -> Option D
Quick Check:
Nonce = Security token for authorization [OK]
Hint: Nonce = security check token to stop bad requests [OK]
Common Mistakes:
Thinking nonce speeds up performance
Confusing nonce with user data storage
Assuming nonce creates backups
2. Which function is used to verify a nonce in WordPress?
easy
A. wp_create_nonce()
B. wp_verify_nonce()
C. check_nonce()
D. nonce_verify()
Solution
Step 1: Recall nonce creation function
wp_create_nonce() is used to create a nonce, not verify it.
Step 2: Identify the correct verification function
wp_verify_nonce() is the official WordPress function to check if a nonce is valid.
Final Answer:
wp_verify_nonce() -> Option B
Quick Check:
Verify nonce = wp_verify_nonce() [OK]
Hint: Verify nonce starts with 'wp_verify_' [OK]
Common Mistakes:
Using wp_create_nonce() to verify
Assuming check_nonce() is a WordPress function
Mixing function names
3. What will the following code output if the nonce is invalid?