【问题标题】:Custom meta box for page template not saving data页面模板的自定义元框不保存数据
【发布时间】:2017-09-20 16:20:18
【问题描述】:

一直在努力寻找解决当前问题的方法。简单地说,我创建了一个'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' );

感谢任何建议。

【问题讨论】:

  • 试试这个 add_action('save_post ','page_save_custom_post_meta');
  • 谢谢你,阿克谢!不幸的是,即使添加了“保存帖子”操作,它仍然不会保存。我不太确定是否有“save_page”操作,但我不相信有。
  • 您能否将输入类型名称 some-string 更改为 somestring,请在请求区域进行相应更改。我不确定这个建议

标签: php wordpress metadata custom-post-type


【解决方案1】:

以后遇到这种情况的人您好。我能够让它与下面的代码一起工作,有人可能会跟进一个更好的答案来解释它为什么工作。我的想法是,将我的代码更新为“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' );

【讨论】:

    【解决方案2】:

    试试下面的代码:

    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');
    

    【讨论】:

    • 谢谢,阿克谢!我删除了其他 'add_action' 呃,动作并根据您的建议进行了更新。可悲的是,内容仍然没有保存。出于好奇,我确实尝试将我的元框从页面模板移动到自定义帖子类型,但它仍然没有保存。所以很有可能这是我的代码中的一些东西,而不是 WP 上的一个奇怪的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多