1

I am new in using Nonce.I am using Nonce like below in a Form

wp_nonce_field('add_new_addres','add_new_address'); 

I am trying to verify Nonce like below

    if(isset( $_REQUEST['action'] ) && ('newAddress' === $_REQUEST['action']) && (wp_verify_nonce($_REQUEST['add_new_address'], 'add_new_addres'))) {
    
         //more code here
    
    }else {
         //more code here
         
    }

else block is executing but I need to execute if block.

1 Answer 1

2

Problem is, you are submitting data as POST data, but verifying nonce from GET data.

Here is how you can create a nonce field in a form easily:

wp_nonce_field( 'add_new_addres' );

Actually, I personally don't use more than 1 parameter when calling the wp_nonce_field function.

Then when verify use the following code:

if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'add_new_addres' ) ) {
    wp_die( 'Are you cheating?' );
    // Anything that you want to display for unauthorized action
} 

// Good to go for next step
2
  • Thanks You vai @Shah Alom.
    – abu abu
    Aug 29, 2020 at 15:43
  • 1
    Cheers Brother!
    – Shah Alom
    Aug 29, 2020 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.