Skip to main content
Added post link
Source Link
Kumar
  • 2.9k
  • 18
  • 19

That should do it. Corresponding Post: http://codechutney.com/auto-resize-wp-editor-custom-instance/

That should do it.

That should do it. Corresponding Post: http://codechutney.com/auto-resize-wp-editor-custom-instance/

Source Link
Kumar
  • 2.9k
  • 18
  • 19

Finally I got around this.

You need to modify tinymce args passed to wp_editor function. WordPress have a argument wp_autoresize_on to allow the editor to be resized automatically.

So instead of these:

'tinymce' => array(
         'toolbar1'=> 'bold,italic,underline,link,unlink,forecolor',
         'toolbar2' => '',
         'toolbar3' => ''
     )

you need to use this:

'tinymce' => array(
        'autoresize_min_height' => 100,
        'wp_autoresize_on'      => true,
        'plugins'               => 'wpautoresize',
        'toolbar1'              => 'bold,italic,underline,link,unlink,forecolor',
        'toolbar2'              => '',
    ),

There are two additional args in here, autoresize_min_height, you can set it to desired height, and second one is 'wp_autoresize_on' => true,. Apart from that you need to pass additional parameter to load the tinymce plugin for auto resizing and i.e 'plugins' => 'wpautoresize' and the auto resizing works flawlessly.

With these changes, I'd suggest you add some other checks in your code. Like for the function:

function xxx_textarea_save_fields_metabox($post_id, $post) {
  global $post_id;

  if ( !wp_verify_nonce( $_POST['xxx_textarea_noncename'], plugin_basename(__FILE__) )){return $post->ID;}  
  if ( !current_user_can( 'edit_post', $post->ID ))                                      {return $post->ID;}   
  if( $post->post_type == 'revision' )                                                 {return;$post->ID;}

  if($_POST['_xxx_textarea']) {$xxx_content = $_POST['_xxx_textarea'];}
  else{$xxx_content = '';};

  if(get_post_meta($post_id, '_xxx_textarea', FALSE))
  {update_post_meta($post_id, '_xxx_textarea', $xxx_content);} 
  else{ add_post_meta($post_id, '_xxx_textarea', $xxx_content);};             

  }

Make sure to add a check for empty $_POST, otherwise you'll get notices on post edit screen. I've made the changes and formatted the code ( You should be doing that ), here is the whole code for adding metabox.

add_action( 'add_meta_boxes', 'xxx_textarea_add_fields_metabox' );
function xxx_textarea_add_fields_metabox() {
    add_meta_box(
        'xxx_textarea_metabox',
        'Intro',
        'xxx_textarea_show_fields_metabox',
        'new_xxx',
        'side',
        'default'
    );
}

function xxx_textarea_show_fields_metabox() {
    global $post;
    $content = get_post_meta( $post->ID, '_xxx_textarea', true );
    //Loads the editor to allow adding fresh content if there is no content already
    $content = empty( $content ) ? '' : $content;

    $args = array(
        'description_name' => 'xxx_textarea',
        'teeny'            => true,
        'quicktags'        => false,
        'media_buttons'    => false,
        'tinymce'          => array(
            'autoresize_min_height' => 100,
            'wp_autoresize_on'      => true,
            'plugins'               => 'wpautoresize',
            'toolbar1'              => 'bold,italic,underline,link,unlink,forecolor',
            'toolbar2'              => '',
        ),
    );
    wp_editor( $content, '_xxx_textarea', $args );
    wp_nonce_field( plugin_basename( __FILE__ ), 'xxx_textarea_noncename' );
}

add_action( 'save_post', 'xxx_textarea_save_fields_metabox', 1, 2 );
function xxx_textarea_save_fields_metabox( $post_id, $post ) {
    global $post_id;

    //Avoids notice and warnings
    if( empty( $_POST ) ) {
        return $post->ID;
    }
    if ( !empty( $_POST['xxx_textarea_noncename'] ) && ! wp_verify_nonce( $_POST['xxx_textarea_noncename'], plugin_basename( __FILE__ ) ) ) {
        return $post->ID;
    }
    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
        return $post->ID;
    }
    if ( $post->post_type == 'revision' ) {
        return;
        $post->ID;
    }

    if ( $_POST['_xxx_textarea'] ) {
        $xxx_content = $_POST['_xxx_textarea'];
    } else {
        $xxx_content = '';
    };

    if ( get_post_meta( $post_id, '_xxx_textarea', false ) ) {
        update_post_meta( $post_id, '_xxx_textarea', $xxx_content );
    } else {
        add_post_meta( $post_id, '_xxx_textarea', $xxx_content );
    };
}

That should do it.