Php 页面模板的自定义元框不保存数据

Php 页面模板的自定义元框不保存数据,php,wordpress,metadata,custom-post-type,Php,Wordpress,Metadata,Custom Post Type,我一直在拼命想办法解决我目前的问题。简单地说,我创建了一个“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(

我一直在拼命想办法解决我目前的问题。简单地说,我创建了一个“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' );
如有任何建议,我们将不胜感激

尝试以下代码:

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

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

尝试此添加操作“保存帖子”和“页面保存自定义帖子”;谢谢你,阿克谢!不幸的是,即使在添加了“保存帖子”操作之后,它仍然不会保存。我不太确定是否有“保存页面”操作,但我不相信有。能否将您的输入类型名称从some string更改为some string,并请在请求区域相应更改。我不确定这个建议是否正确谢谢Akshay!我删除了另一个“添加操作”呃,操作,并根据您的建议进行了更新。遗憾的是,内容仍然没有保存。出于好奇,我尝试将我的元框从页面模板移动到自定义帖子类型,但它仍然没有保存。所以这很有可能是我的代码中的某个东西,而不是WP上的一个奇怪错误。