How to use nonce with front end submission form?

Use the following code inside just before tag on your front end code.

wp_nonce_field('name_of_your_action', 'name_of_your_nonce_field');

The above code will generate two hidden inputs inside your form tag. Now you can verify your nonce in the backend where you will process your form. Use the following code to verify the nonce you just created above.

  if(wp_verify_nonce($_REQUEST['name_of_your_nonce_field'], 'name_of_your_action')){

            // Nonce is matched and valid. do whatever you want now.

     } else {

           // Invalid nonce. you can throw an error here.
}

One note: see carefully how “name_of_your_action” is the first argument of ‘wp_nonce_field()‘ function and it is the second argument of ‘wp_verify_nonce()‘ function. 🙂 So, do not use it as the first argument in both functions. Many people make this mistake so I wanted to mention.

Leave a Comment