Metabox nonce PHP notice

时间:2012-04-03 作者:Beto

当我尝试在两种不同的分类法中添加新帖子时,我收到了一条通知,其中一种我使用自定义元框,另一种则不使用,但是当我尝试添加新项目时,这两种分类法都会出现通知,我看到:通知:未定义索引:product\\u noncename in/Applications/MAMP/htdocs/site/wordpress/wp-content/themes/theme/functions。第259行中的php隐藏在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 个回复
最合适的回答,由SO网友:Rajeev Vyas 整理而成
// 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
SO网友:Bainternet

简单检查是否$_POST[\'product_noncename\'] 首先设置,因此更改此行:

if ( !wp_verify_nonce( $_POST[\'product_noncename\'], plugin_basename(__FILE__) )) {
对此:

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

SO网友:Luke Keller

根据班纳特的回答,这对我很有用:

if ( !isset($_POST[\'product_noncename\']) || !wp_verify_nonce( $_POST[\'product_noncename\'], basename(__FILE__) )) {
请注意plugin\\u basename与basename的区别。

结束

相关推荐

定制MetaBox:食品菜单项可用日期

我希望创建一个带有复选框的自定义元数据库,用于显示项目可用性。我已经包括了两张我希望实现的示例图像。任何帮助都将不胜感激。此外,我真的只是想知道从哪里开始解决这个问题。有关更多信息,我将创建一个特定于菜单项的自定义帖子类型。这是给当地一家纸杯蛋糕面包店的。谢谢http://brocknunn.com/help-pics/Menu-Dates.jpghttp://brocknunn.com/help-pics/checkbox.png