0

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

?>

2 Answers 2

0

You forgot to declare the variable $main_detail. Since it's a meta field you need to retrieve it first before you add it.

$main_detail = get_post_meta($post->ID, '_main_detail', true);  

Also, I can see that some errors in your code.

echo '<label >';?><?php _e( 'short description:' );?></label>

Should be..

echo '<label >' . _e( "short description:" ) . '</label>';

And, you also need to add the key _main_detail in your $performer_meta array in order to save it.

1

In your wpt_save_performer_meta() function you are iterating through $performer_meta array and saving its values. But it has only one manually set key (_short_description) ... and its value is result of isset($_POST['_short_description']), which is probablay always true, and saved (or displayed) as 1.

So it should be like this:

$performer_meta['_short_description'] = trim($_POST['_short_description']);

But your code will not save _main_detail editor field, because its key does not exist in $performer_meta array.

Edit: Notice: Undefined variable: main_detail ... is there because the variable $main_detail is missing, but you are using it in wp_editor().

2
  • thanks for helping , if i do as you say then it throws lot of notices. So the main problem is nonce. The nonce i am using is for plugin usage. But my code is inside the theme. So i guess thats the issue.
    – terminator
    Commented Aug 11, 2015 at 7:32
  • Notice: Undefined variable: main_detail ... notice is there because the variable $main_detail is missing, but you are using it in wp_editor() ...
    – Marek
    Commented Aug 11, 2015 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.