dss89001 2014-12-11 01:45
浏览 56
已采纳

自定义MetaBoxes不保存

I've been building a custom post type and want to include a custom meta box but I can't get it to save.

This is my custom post type function which works

// Custom post type function
function custom_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Models', 'Post Type General Name', 'metro' ),
        'singular_name'       => _x( 'Model', 'Post Type Singular Name', 'metro' ),
        'menu_name'           => __( 'Models', 'metro' ),
        'parent_item_colon'   => __( 'Parent Model', 'metro' ),
        'all_items'           => __( 'All Models', 'metro' ),
        'view_item'           => __( 'View Model', 'metro' ),
        'add_new_item'        => __( 'Add New Model', 'metro' ),
        'add_new'             => __( 'Add New', 'metro' ),
        'edit_item'           => __( 'Edit Model', 'metro' ),
        'update_item'         => __( 'Update Model', 'metro' ),
        'search_items'        => __( 'Search Model', 'metro' ),
        'not_found'           => __( 'Not Found', 'metro' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'metro' ),
    );

// Set other options for Custom Post Type

    $args = array(
        'label'               => __( 'Models', 'metro' ),
        'description'         => __( 'Model news and reviews', 'metro' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'revisions' ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        //'taxonomies'          => array( 'genres' ),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        'hierarchical'            => false,
        'public'                  => true,
        'show_ui'                 => true,
        'show_in_menu'            => true,
        'show_in_nav_menus'       => true,
        'show_in_admin_bar'       => true,
        'menu_position'           => 5,
        'can_export'              => true,
        'has_archive'             => true,
        'exclude_from_search'     => false,
        'publicly_queryable'      => true,
        'capability_type'         => 'post',
        'register_meta_box_cb'    => 'add_models_metaboxes'
    );

    // Registering your Custom Post Type
    register_post_type( 'Models', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type' );

This is the metabox code

/**
 * Custom MetaBoxes
 */

function add_models_metaboxes() {
    add_meta_box('tj_modeldetails', 'Model Details', 'tj_modeldetails', 'models', 'normal', 'high');
}

// The Event Location Metabox

function tj_modeldetails() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

    // Get the location data if its already been entered
    $dress_size = get_post_meta($post->ID, '_dress_size', true);
    $height     = get_post_meta($post->ID, '_height', true);
    $bust       = get_post_meta($post->ID, '_bust', true);
    $waist      = get_post_meta($post->ID, '_waist', true);
    $hips       = get_post_meta($post->ID, '_hips', true);
    $shoe       = get_post_meta($post->ID, '_shoe', true);
    $hair       = get_post_meta($post->ID, '_hair', true);
    $eyes       = get_post_meta($post->ID, '_eyes', true);

    // Echo out the field
    echo '<p>Dress Size:</p>';
    echo '<input type="text" name="_dress_size" value="' . $dress_size  . '" class="widefat" />';

    echo '<p>Height:</p>';
    echo '<input type="text" name="_height" value="'. $height . '" class="widefat" />';

    echo '<p>Bust:</p>';
    echo '<input type="text" name="_bust" value="'. $bust . '" class="widefat" />';

    echo '<p>Waist:</p>';
    echo '<input type="text" name="_waist" value="'. $waist . '" class="widefat" />';

    echo '<p>Hips:</p>';
    echo '<input type="text" name="_hips" value="'. $hips . '" class="widefat" />';

    echo '<p>Shoe:</p>';
    echo '<input type="text" name="_shoe" value="'. $shoe . '" class="widefat" />';

    echo '<p>Hair:</p>';
    echo '<input type="text" name="_hair" value="'. $hair . '" class="widefat" />';

    echo '<p>Eyes:</p>';
    echo '<input type="text" name="_eyes" value="'. $eyes . '" class="widefat" />';


}

function tj_save_models_meta($post_id, $post) { 
    // verify this came from our screen with proper authorisation.
    if ( !wp_verify_nonce( $_POST['modelmeta_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
    }

    // is user allowed to edit the post or page
    if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;

    // Put metaboxes into an array

    $models_meta['_dress_size'] = $_POST['_dress_size'];
    $models_meta['_height']     = $_POST['_height'];
    $models_meta['_bust']       = $_POST['_bust'];
    $models_meta['_waist']      = $_POST['_waist'];
    $models_meta['_hips']       = $_POST['_hips'];
    $models_meta['_shoe']       = $_POST['_shoe'];
    $models_meta['_hair']       = $_POST['_hair'];
    $models_meta['_eyes']       = $_POST['_eyes'];

    foreach ( $models_meta as $key => $value ) {
        if( $post->post_type == 'revision' ) return; // don't store custom data twice
        #$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)

        if( get_post_meta($post->ID, $key, FALSE) ) {
            update_post_meta($post->ID, $key, $value);
        } else {
            add_post_meta($post->ID, $key, $value);
        }

        if(!$value) delete_post_meta($post->ID, $key);
    }
}

add_action('save_post', 'tj_save_models_meta', 1, 2);

While the metaboxes do show in the post window when I click publish the values aren't saved. Any idea what I'm missing?

  • 写回答

1条回答 默认 最新

  • doucai4274 2014-12-11 06:53
    关注

    Verify nonce should be failing, having had a look at the code the nonce field is called "eventmeta_noncename"

    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename"

    The one being verified is "modelmeta_noncename"

    if ( !wp_verify_nonce( $_POST['modelmeta_noncename'],

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分