2014-02-24 28 views
0

我一直在試圖讓我的自定義元框數據保存在Wordpress中,並且我沒有任何運氣。我試着研究其他帖子,但由於每個人都做了一點不同,所以我沒有成功地使用教程和其他帖子。如何在Wordpress中保存自定義元框

我創建了一個metabox:

add_action('add_meta_boxes', 'ic_add_heading_box'); 

function ic_add_heading_box($post) { 

    add_meta_box(
      'Meta Box', 
      'Heading Titles', 
      'ic_heading_box_content', 
      'page', 
      'normal', 
      'high' 
     ); 

} 

function ic_heading_box_content($post) { 

    echo '<label>Main Heading (h1)</label>'; 
    echo '<input type="text" name="heading_box_h1" value="" />'; 
    echo '<label>Sub Heading (h3)</label>'; 
    echo '<input type="text" name="heading_box_h3" value="" />'; 

} 

我不能爲我的生命得到我插入到字段中的數據在WordPress的保存。任何幫助將不勝感激。

+1

看看文件上,有一個很好的例子,從開始到結束。 http://codex.wordpress.org/Function_Reference/add_meta_box –

回答

2

您正在使用的功能只是一個顯示功能。 你實際上沒有對數據做任何事情。它僅用於創建metabox。不處理它。

您需要添加

add_action('save_post', 'myplugin_save_postdata'); 

,然後在codex example使用update_post_meta()與功能,如:

function myplugin_save_postdata($post_id) { 

    // If this is an autosave, our form has not been submitted, so we don't want to do anything. 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 

    // Check the user's permissions. If want 
    if ('page' == $_POST['post_type']) { 

    if (! current_user_can('edit_page', $post_id)) 
     return $post_id; 

    } else { 

    if (! current_user_can('edit_post', $post_id)) 
     return $post_id; 
    } 

    /* OK, its safe for us to save the data now. */ 

    // Sanitize user input. if you want 
    $mydata = sanitize_text_field($_POST['myplugin_new_field']); 

    // Update the meta field in the database. 
    update_post_meta($post_id, '_my_meta_value_key', $mydata); // choose field name 
} 
+0

感謝您的迴應,我將該代碼添加到了我的文件中,而且顯然不起作用,因爲我需要編輯其中一些變量和值。我似乎仍然無法編輯正確的字段。有沒有什麼辦法可以基於我的代碼更具體一些? –

相關問題