0

I'm adding a metabox on a custom post type and my nonce-verification fails. I'm not really where to look for why it's going wrong. Can anyone point me in the right direction? Here's my code:

function myplugin_add_metabox() {
     add_meta_box(
         'myplugin_box', // $id
         'Some Title', // $title 
         'myplugin_box', // $callback
         'custom_post_type', // $page
         'normal', // $context
         'high'); // $priority
 }
 add_action('add_meta_boxes', 'myplugin_add_metabox');


function myplugin_box() {
  echo '<input type="hidden" name="myplugin_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
}


function myplugin_save_box() {
  // verify nonce
  if (!wp_verify_nonce($_POST['myplugin_nonce'], basename(__FILE__)))  {
     error_log("nonce verification failed", 0);
     return $post_id;
  }
  error_log("nonce worked", 0);
}
 add_action('save_post', 'myplugin_save_box');

Now, unfortunately I'm reading "nonce verification failed" in my log and not "nonce worked". Does anyone have an idea where to look for the error?

Thanks!

0

Ok, I found the answer. It's a quite stupid copy/paste mistake. The name of my nonce_field was not unique. I had already created another nonce from the same plugin but another file. Therefore the basename(__FILE__) value was different leading to a different nonce. All I had to do was change the the name of the field to something unique and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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