子主题中的 WordPress 未定义索引通知


Wordpress undefined index notice in child theme

当我在wordpress子主题中将项目添加到菜单中时,出现此错误;

Notice: Undefined index: custom_meta_box_nonce in /Users/x/Documents/Apart-1/website 3/wp-content/themes/wp-foundation/functions.php on line 546

我的完整基本主题功能文件的链接在这里;(同样,我没有触摸/写/那个。http://codepad.org/dAx0DlLz

这是我基本主题中的第 546 行左右;

// Save the Data  

函数 save_homepage_meta($post_id) {
全局$custom_元_字段;

// verify nonce  
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))  
    return $post_id;  
// check autosave  
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
    return $post_id;  
// check permissions  
if ('page' == $_POST['post_type']) {  
    if (!current_user_can('edit_page', $post_id))  
        return $post_id;  
    } elseif (!current_user_can('edit_post', $post_id)) {  
        return $post_id;  
}  

这正好在第 546 行;

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

这是我的孩子主题的功能文件中;

    <?php
ob_start();
if ( function_exists( 'add_image_size' ) ) { add_image_size( 'orbit-custom', 920, 300 ); }
/**
 * Add a search bar to the navigation menu.
 *
 * @since Twenty Twelve 1.0
 */
function menu_search($items){
    $search = '<li class="menusearch">';
    $search .= '<form method="get" id="searchform" action="/">';
    $search .= '<input type="text" class="field" name="s" id="s" placeholder="Search" />';
    $search .= '<input type="submit" class="menusubmit" name="submit" id="searchsubmit" value="Search" />';
    $search .= '</form>';
    $search .= '</li>';
    return $items . $search;
}
add_filter('wp_nav_menu_items','menu_search');
// This adds multiple menu locations
add_action( 'init', 'register_multiple_menus' );
function register_multiple_menus() {
    register_nav_menus(
        array(
            'footer-nav-mid' =>  'Middle Footer Navigation',
            'footer-nav-left' =>  'Left Footer Navigation',
            'footer-nav-right' =>  'Right Footer Navigation'
        )
    );
}
 ?>

为什么我会收到此错误?我没有对基本主题的功能文件进行任何更改,并且在将多个菜单内容添加到我孩子的功能文件之前没有收到此错误。

对于undefined index通知,解决方案通常使用 isset()

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