2017-09-20 49 views
0

在我试图找出当前问题的解决方案时,我的头撞墙。简而言之,我创建了一个'page-home.php'模板页面,可以获取一个元框以调用页面模板,但尝试使用元框中的数据更新页面会使数据消失。下面不保存数据的页面模板的自定义元框

代码:

function page_add_meta_boxes($post) { 
    global $post; 

    if(!empty($post)) 
    // get the page template post meta 
    $page_template = get_post_meta($post->ID, '_wp_page_template', true); 
    // if the current page uses our specific template, then output our custom metabox 
    { 
     $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true); 
     // looks for page-home.php file to add our meta box 
     if($pageTemplate == 'page-home.php') 
     { 
      add_meta_box(
       'page-custom-metabox', // $id 
       'Special Post Meta', // $title 
       'page_template_metabox', // $callback 
       'page', // $page 
       'normal', // $context 
       'high'); // $priority 
     } 
    } 
} 
add_action('add_meta_boxes_page', 'page_add_meta_boxes'); 


function page_template_metabox() { 
    wp_nonce_field(basename(__FILE__), 'page_meta_box_nonce'); 
    $some_string = get_post_meta($post->ID, '_some_string', true); 
    ?> 
    <input type="text" name="some-string" value="<?php echo $some_string; ?>" /> 
    <?php 
} 


function page_save_custom_post_meta() { 
    if (!isset($_POST['page_meta_box_nonce']) || !wp_verify_nonce($_POST['page_meta_box_nonce'], basename(__FILE__))){ 
     return; 
    } 
    // return if autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ 
     return; 
    } 
    // check the user's permissions. 
    if (! current_user_can('edit_page', $post_id)){ 
     return; 
    } 

    // save our string 
    if (isset($_REQUEST['some-string'])) { 
     update_post_meta($post_id, '_some_string', sanitize_text_field($_POST['some-string'])); 
    } 
} 
add_action('publish_page', 'page_save_custom_post_meta'); 
add_action('draft_page', 'page_save_custom_post_meta'); 
add_action('future_page', 'page_save_custom_post_meta'); 

任何建议都理解的。

+0

尝试用这种ADD_ACTION(“save_post”,“page_save_custom_post_meta”); –

+0

谢谢Akshay!不幸的是,即使在添加“保存帖子”操作后,它仍然不会保存。我不太确定是否有'save_page'动作,但我不相信。 – Leroy

+0

您可以将您的输入类型名称some-string更改为somestring,并请相应地在请求区域中进行更改。我不确定这个建议 –

回答

0

你好人谁在将来运行到这一点。我能够使它与下面的代码一起工作,有人可能能够更好地回答它为什么会起作用。我的想法是,将我的代码更新为'save_post_page'而不是'save_post'会产生差异。 (请注意我只是改变了一些信息,由于想在我的主题进行测试):

// Add meta box 
function frontpage_meta_boxes($post){ 
    global $post; 

    if(!empty($post)) 
    $page_template = get_post_meta($post->ID, '_wp_page_template', true); 
    { 
     $pageTemplate = get_post_meta($post->ID, '_wp_page_template', true); 

     if($pageTemplate == 'page-home.php') 
     { 
      add_meta_box('frontpage_meta_box', __('Features'), 'frontpage_meta_box', 'page', 'advanced', 'high'); 
     } 
    } 
} 
add_action('add_meta_boxes_page', 'frontpage_meta_boxes'); 

// builds our meta box 
function frontpage_meta_box($post){ 
    // make sure the form request comes from WordPress 
    wp_nonce_field(basename(__FILE__), 'frontpage_meta_box_nonce'); 
    // retrieve the _manuf_url current value 
    $manufacturer_url = get_post_meta($post->ID, '_manuf_url', true); 

    ?> 
     <h3><?php _e('Manufacturer URL'); ?></h3> 
      <p> 
       <input type="text" name="manufacturer-url" value="<?php echo $manufacturer_url; ?>" /> 
      </p> 

    <?php 
} 

// saves our data 
function frontpage_save_meta_box_data($post_id){ 
    // verify meta box nonce 
    if (!isset($_POST['frontpage_meta_box_nonce']) || !wp_verify_nonce($_POST['frontpage_meta_box_nonce'], basename(__FILE__))){ 
     return; 
    } 
    // return if autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ 
     return; 
    } 
    // Check the user's permissions. 
    if (! current_user_can('edit_post', $post_id)){ 
     return; 
    } 
    // manufacturer url string 
    if (isset($_REQUEST['manufacturer-url'])) { 
     update_post_meta($post_id, '_manuf_url', sanitize_text_field($_POST['manufacturer-url'])); 
    } 
    // store custom fields values 
} 
add_action('save_post_page', 'frontpage_save_meta_box_data'); 
0

尝试用下面的代码:

function page_save_custom_post_meta($postid) { 
if (!isset($_POST['page_meta_box_nonce']) || !wp_verify_nonce($_POST['page_meta_box_nonce'], basename(__FILE__))){ 
    return; 
} 
// return if autosave 
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ 
    return; 
} 
// check the user's permissions. 
if (! current_user_can('edit_page', $post_id)){ 
    return; 
} 

// save our string 
if (isset($_REQUEST['some-string'])) { 
    update_post_meta($post_id, '_some_string', sanitize_text_field($_POST['some-string'])); 
    } 
} 


add_action('save_post ','page_save_custom_post_meta'); 
+0

谢谢,Akshay!我删除了其他'add_action'呃,动作并根据你的建议更新。可悲的是,内容仍然没有保存。出于好奇,我尝试将我的元框从一个页面模板移动到一个自定义的帖子类型,但它仍然没有保存。所以这很有可能是我的代码中的东西,而不是WP上的奇怪错误。 – Leroy