admin管理员组

文章数量:1125609

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.

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.

Share Improve this question asked Aug 29, 2020 at 11:20 abu abuabu abu 2031 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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

本文标签: Verify a nonce in Form submission