2

I am trying to debug 'undefined index' errors on the WP backend when debug is set to TRUE. The offending lines are:

if ( !wp_verify_nonce( $_POST['pdwt_meta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
}

Which I have then changed to check whether the value has been set by adding:

if ( isset( $_POST['pdwt_meta_noncename']) || ! wp_verify_nonce( $_POST['pdwt_meta_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
}

The addition makes the error disappear but when I enter information on the custom field, it does not save.

This is how the custom field has been set:

echo '<input type="hidden" name="pdwt_meta_noncename" id="pdwt_meta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

Here is the save_post part:

foreach ($pdwt_meta as $key => $value) { // loop through the $pdwt_meta array
    if( $post->post_type == 'revision' ) return; // make sure the data isn't stored twice
    $value = implode(',', (array)$value); // if $value is in an array, make it a comma seperated value
    if(get_post_meta($post->ID, $key, FALSE)) { // if the custom field already has a value
        update_post_meta($post->ID, $key, $value);
    } else { // AND IF IT DOESN'T ALREADY HAVE A VALUE
        add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // DELETE IF IT'S EMPTY
}     
add_action('save_post', 'pdwt_save_meta', 1, 2); // SAVE THE CUSTOM FIELDS 

I did not write this specific part of the theme. Any thoughts on what could be wrong?

4
  • I'm also having the same problem. Till can't find any solution. For the volunteers, here's the exact tuts link that the process is revised: code.tutsplus.com/articles/… Commented Oct 15, 2014 at 4:19
  • 1
    @mayeenul Ah! thanks for this! Have you tried applying the solution suggested on the comments on this tutorial. It looks like I was missing a line on the save_post part by the look of it for this line: wp_nonce_field( basename( __FILE__ ), 'custom_meta_box_nonce' ); In function save_meta() replace this line: if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) for this line: if (!isset($_POST['custom_meta_box_nonce']) || !wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
    – nivims
    Commented Nov 21, 2014 at 16:55
  • it worked. You can add this as answer mentioning the comment as a link with a basic explanation, and then can accept it as an answer. It'll help others. :) Commented Feb 10, 2015 at 9:57
  • Great you got it working @MayeenulIslam. Have now added it as an answer. Cheers!
    – nivims
    Commented Feb 11, 2015 at 15:58

1 Answer 1

2

Resolved by following comment on Tutsplus Tutorial (link included):

In function save_meta(), replace this line:

if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))

with this line:

if (!isset($_POST['custom_meta_box_nonce']) || !wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))

See full comment here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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