2016-01-13 60 views
1

我想添加一个字段的自定义元框到我创建的自定义帖子类型,使用以下教程:Creating a metabox in wordpress 。它一切正常,但是困扰我的是当我开始新的自定义帖子类型帖子时的错误消息。验证wordpress wp_verify_nonce

当我点击我的自定义创建的职位类型组合新的,我得到这个消息

Notice: Undefined index: mytheme_meta_box_nonce in /home/jellyf1q/public_html/test/webfanatics/wp/wp-content/themes/thewebfanatics/cpt/portfolio.php on line 140

功能还正常工作,一旦我更新它,离开后,回到它编辑它,消息不见了。

行140指向现时。它通过授权授权来检查日期是否来自编辑帖子。下面是该行的代码:

function mytheme_save_data($post_id) { 
    global $meta_box; 

// verify nonce 
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) { 
     return $post_id; 
    } 
    .... more code .... 
} 

我的假设是,当我创建一个新的职位元框的现时尚未建立,因而不能进行检查,还是我错在这里?有没有解决这个错误信息?

我知道我可以打开wordpress的调试模式,但我在这种情况下寻找更多解决方案。

回答

2

更改您的代码,如下所示,只需检查$_POST['mytheme_meta_box_nonce']是否已设置。

function mytheme_save_data($post_id) { 
    global $meta_box; 

    // verify nonce 
    if (isset($_POST['mytheme_meta_box_nonce']) && !wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) { 
     return $post_id; 
    } 
    .... more code .... 
}