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've added several meta boxes to post and the function that suppose to save those boxes.

When i save menus ( appearance -> menu). it throws a warning for each added menu or sub menu Notice: Undefined index: image_price_variation in /home1/xxx/public_html/xx/wp-content/plugins/xxx-shopping-cart/xxx_metabox_functions.php on line 89

Although i am saving menus and not posts.

Here is a short version of the code:

function add_custom_meta_boxes() {  

/*  Adding price meta box to post   */
add_meta_box(  
    'oap_price_box',  
    'Product Price',  
    'oap_price_mockup',  
    'post',  
    'side',
    'high'
); 

// ... 
}
add_action('add_meta_boxes', 'add_custom_meta_boxes');

/* Prints the price  html mockup */
 function oap_price_mockup( $post ) {
wp_nonce_field( plugin_basename( __FILE__ ), 'image_price_variation' );
$productPrice = (double)get_post_meta( $post->ID, 'productPrice', true );
$html  = '$ <input type="text" size="7" name="productPrice" placeholder="price" value="'.(($productPrice != '') ? $productPrice : '').'" />';    
echo $html;
}

function save_meta_boxes_post_data( $post ) {
    // verification
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
    return;

if ( !wp_verify_nonce( $_POST['image_price_variation'], plugin_basename( __FILE__ ) ) ) // - This is line 89 ..
    return;

if ( 'post' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_post', $post ) )
        return;
}

$post_ID = $_POST['post_ID'];

/*  Saveing Price*/
if(isset($_POST['productPrice']) && $_POST['productPrice'] != ''){
    $price = (double)sanitize_text_field( $_POST['productPrice'] );
    update_post_meta($post_ID, 'productPrice', $price);
}

}
add_action( 'save_post', 'save_meta_boxes_post_data' );

The error comes from the nonce field not being presented however doesn't WP separate between post saving to menu saving ? if so is there something i could do to change mine to do that?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Check in your function to save the post meta data for the post type or better for $current_screen. The var is a global and return a object with different identifier to check, in which admin page init the function. Check in your function and return if is not the right $current_screen->id or post_type.

Small hint: use this plugin for easy identify the var.

Also a example for check the current screen and if not the right, then return Null. The follow source is inside a function. But the var have more items inside the array, use the best for your requirement.

global $current_screen;

if ( isset( $current_screen->id ) && 
    'edit.php' === $current_screen->parent_file ) // check, if on edit post type page
 )
 return NULL;

Background: WordPress use the post type and his hooks also on other types, like attachments and nav menu.

share|improve this answer
    
I think i am already checking which post type it is. if ( 'post' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_post', $post ) ) return; } should i add else return ? –  Yaniv Kossas Jan 21 '13 at 22:43
    
no, you check the data from the $_post, is always active, if the form tag send data. –  bueltge Jan 21 '13 at 22:54
    
I see. and how can i check for the current data then ? –  Yaniv Kossas Jan 21 '13 at 23:38
    
A example plugin, there add a meta box. See the structure on the __construct(). This makes it easy to control it. Also a small update on my answer with code for check the $current_screen. –  bueltge Jan 23 '13 at 12:07
    
Thank you very much @bueltge –  Yaniv Kossas Jan 23 '13 at 15:09

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.