wordpress - wp_verify_nonce not working, whereas wp_create_nonce works -


i surprised why wp_verify_nonce not working. it's showing undefined function error, wordpress version upto date. attaching plugin code. please me

    add_shortcode('tw_safety_checklist_template','init_tw_safety_checklist');      function init_tw_safety_checklist(){         echo '<form method="post">             <label>name</label>             <input type="hidden" name="tw_new_checklist_nonce" value="'.wp_create_nonce('tw_new_checklist_nonce').'"/>             <input type="text" name="tw_name" />             <input type="submit" name="submit" value="submit"/>         </form>';     }      if(isset($_post['tw_new_checklist_nonce'])){         tw_create_my_template();                 }       function tw_create_my_template(){         if(wp_verify_nonce($_post['tw_new_checklist_nonce'],'tw-new-checklist-nonce'))         {             return 'worked!';         }     } 

the issue wp_verify_nonce() pluggable function. means not declared until after plugins loaded. since if statement loose in file, being executed when plugin loads; such, wp_verify_nonce() (correctly) has not been declared yet.

you need move if statement action hook using add_action(). hook depend on purpose of tw_create_my_template() function is. you'll want this:

add_action('init','tw_create_my_template'); function tw_create_my_template(){     if( isset($_post['tw_new_checklist_nonce'])        && wp_verify_nonce($_post['tw_new_checklist_nonce'],'tw-new-checklist-nonce'))     {         return 'worked!';     } } 

note, you'll want replace init whatever hook appropriate function. init typical plugin initialization actions important thing is happens after plugins_loaded. can find list of typical actions, in order, here.


Comments