I have done this so many times but facing problems with meta boxes and wp_editor these days, lot of notices, errors and strange behaviour.
I have been able to solve problem with meta boxes notices and save . The main problem was nonce. The nonce i was using was for plugin usage. But my code is inside the theme so now i am unable to save the value in metabox and no more notices for metaboxes.
But now i get notice Notice: Undefined variable: main_detail
in for wp_editor.
And it doesn't save the value after i hit publish/update
<?php
function wpt_performer_posttype() {
register_post_type( 'performer',
array(
'labels' => array(
'name' => __( 'Performer' ),
'singular_name' => __( 'performer' ),
'add_new' => __( 'Add New performer' ),
'add_new_item' => __( 'Add New performer' ),
'edit_item' => __( 'Edit performer' ),
'new_item' => __( 'Add New performer' ),
'view_item' => __( 'View performer' ),
'search_items' => __( 'Search performer' ),
'not_found' => __( 'No performer found' ),
'not_found_in_trash' => __( 'No performer found in trash' )
),
'public' => true,
'supports' => array( 'title','thumbnail','page-attributes' ),
'capability_type' => 'post',
'rewrite' => array("slug" => "performer"), // Permalinks format
'menu_position' => 6,
'show_ui'=>true,
'query_var'=>true,
'register_meta_box_cb' => 'add_performer_metaboxes'
)
);
}
add_action( 'init', 'wpt_performer_posttype' );
/*Add featured thumbnails to the custom post type*/
add_theme_support( 'post-thumbnails' );
/*Now we add the meta boxes to the performer*/
// add_action('add_meta_boxes', 'add_performer_metaboxes');
function add_performer_metaboxes() {
add_meta_box('wpt_performer_lines', __('Extra fields'), 'wpt_performer_lines', 'performer', 'normal', 'high');
}
function wpt_performer_lines(){
global $post;
wp_nonce_field( 'my_meta_box', 'performermeta_noncename' );
$short_description = get_post_meta($post->ID, '_short_description', true);
echo '<label >';?><?php _e( 'short description:' );?></label>
<?php echo '<br><textarea name="_short_description" rows="1" cols="90">'.$short_description.'</textarea>';?>
<?php wp_editor( $main_detail, 'main_detail', array( 'textarea_name' => '_main_detail', 'textarea_rows' =>5, 'media_buttons' => false ) );
}
function wpt_save_performer_meta($post_id, $post) {
if ( ! isset( $_POST['performermeta_noncename'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['performermeta_noncename'], 'my_meta_box' ) ) {
return;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
$performer_meta['_short_description'] = trim($_POST['_short_description']);
foreach ($performer_meta as $key => $value) { // Cycle through the $performer_meta array!
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)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
add_action('save_post', 'wpt_save_performer_meta', 1, 2); // save the custom fields
?>