2016-01-13 59 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 .... 
}