自定义MetaBoxes不保存

时间:2014-12-11 01:45:37

标签: php wordpress

我一直在构建自定义帖子类型,并希望包含自定义元框,但我无法保存。

这是我的自定义帖子类型功能

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

这是元箱代码

/**
 * 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);

当我点击发布时,在邮箱窗口中显示元变量时,不会保存值。知道我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

验证nonce应该失败,看一下nonce字段被调用的代码&#34; eventmeta_noncename&#34;

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

正在验证的是&#34; modelmeta_noncename&#34;

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