验证wordpress wp_verify_nonce

时间:2016-01-13 12:30:22

标签: wordpress

我正在尝试使用以下教程将字段添加到我创建的自定义帖子类型的自定义元框:Creating a metabox in wordpress 。这一切都很好,但是当我开始一个新的自定义帖子类型帖子时,困扰我的是错误信息。

当我在自定义创建的帖子类型组合中点击新建时,我收到此消息

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行指的是随机数。它会检查日期是否来自具有propper授权的编辑帖子。以下是该行的代码:

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 ....
}

我的假设是,当我创建一个新帖子时,元框的nonce尚未创建,因此无法检查,或者我在这里错了?是否有解决此错误消息的方法?

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

1 个答案:

答案 0 :(得分: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 ....
}
相关问题