2

I've been getting a notice when I try to add a new post in two different taxonomies, in one i use a custom meta box and in the other not, however the notice appears for both when trying to add a new item, i see this: Notice: Undefined index: product_noncename in /Applications/MAMP/htdocs/site/wordpress/wp-content/themes/theme/functions.php on line 259 hidden just a little below the top black strip in WP Admin

Line 259 is:

if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {

which is part of this:

// Save the Metabox Data
function wpt_save_product_meta($post_id, $post) {
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.
    $product_meta['_location'] = $_POST['_location'];
    $product_meta['_asiaLoc'] = $_POST['_asiaLoc'];
    $product_meta['_europeLoc'] = $_POST['_europeLoc'];
    $product_meta['_isbn'] = $_POST['_isbn'];
    $product_meta['_orderform'] = $_POST['_orderform'];
     // Add values of $events_meta as custom fields
    foreach ($product_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
}
add_action('save_post', 'wpt_save_product_meta', 1, 2); // save the custom fields

3 Answers 3

2
// Save the Metabox Data
function wpt_save_product_meta($post_id, $post) {

    if($post->post_type!='post type with metabox') {
       return $post->ID;
    }

    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }
    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.
    $product_meta['_location'] = $_POST['_location'];
    $product_meta['_asiaLoc'] = $_POST['_asiaLoc'];
    $product_meta['_europeLoc'] = $_POST['_europeLoc'];
    $product_meta['_isbn'] = $_POST['_isbn'];
    $product_meta['_orderform'] = $_POST['_orderform'];
     // Add values of $events_meta as custom fields
    foreach ($product_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == 'revision' ) return; // Don't store custom data twice
        $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn't have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
}
add_action('save_post', 'wpt_save_product_meta', 1, 2); // save the custom fields
0
6

Simplly check if $_POST['product_noncename'] is set first so change this line:

if ( !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {

to this:

if ( !isset($_POST['product_noncename']) || !wp_verify_nonce( $_POST['product_noncename'], plugin_basename(__FILE__) )) {
4
  • Don't you think that checking for isset() will pass even if nonce is not set for particular post type where it is intended to validate nonce.... Apr 3, 2012 at 12:04
  • @RajeevVyas yep, fixed it.
    – Bainternet
    Apr 3, 2012 at 12:07
  • Sorry guys that just generates more notices of undefined indexes for all my custom fields
    – Beto
    Apr 4, 2012 at 5:54
  • Sorry back at you then you have something Wrong with your code, but you can avoid it by checking if each one is set first.
    – Bainternet
    Apr 4, 2012 at 7:13
0

Building off of Bainternet's answer, this worked for me:

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

Notice the difference in plugin_basename vs basename.

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.