Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a new custom post type and some custom fields for it. My function for saving the custom meta fields fires on the 'save_post' hook.

The problem is that I am getting a warning when I go to add a new post:

Notice: Undefined index: my_meta_box_nonce....

       // verify nonce
    if ( ! wp_verify_nonce( $_POST[ 'my_meta_box_nonce' ], basename( __FILE__ ) ) ) 
       return $post_id; 

When I enter the post details and hit publish the error is not displayed, it's only there when I create a new empty post. What's wrong here?

My code is almost identical and based on this tutorial: http://wp.tutsplus.com/tutorials/reusable-custom-meta-boxes-part-1-intro-and-basic-fields/

In the comments of that tutorial it seems that there were others who had the same problem, but there is no solution given.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

I know that this is not the only tutorial that has code that check nonce for meta boxes but this is truly idiotic. Nonce should be checked per the whole action not per parts of it and if your save_post hook was called it means that the save nonce was already checked and found valid, so There is no need for more security checks.

My advice is to simply remove anything related to nonce from your metabox code, but if you will feel better having it around just change the code to

   // verify nonce
    if ( !isset($_POST[ 'my_meta_box_nonce' ]) || !wp_verify_nonce( $_POST[ 'my_meta_box_nonce' ], basename( __FILE__ ) ) ) 
       return $post_id; 

which will bail out when no nonce was supplied or the nonce failed validation

share|improve this answer
    
If I use that code the metaboxes aren't saved, although the notices mentioned don't show any more. –  drtanz Feb 25 '13 at 3:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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