custom metabox nonce verification fails

Question

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
, etiennedi 6 years 2015-05-22T14:26:41-05:00 0 Answers 80 views 0

Leave an answer

Browse
Browse