Auto Resize WP Editor Custom instance

WordPress supports the auto resizing of Post Editor, It’s helpful when your post content grows longer so that you don’t have to keep scrolling. You can use the same Auto resize WP Editor instance in a metabox or a widget.

If you’re using a custom instance of WP Editor with the help of wp_editor() function, you just need to pass a few additional arguments to enable this neat feature.

Here is the code to add a instance of WP Editor in a metabox with auto resizing turned on.

/* Define the custom box */
add_action( 'add_meta_boxes', 'my_meta_box' );

/* Do something with the data entered */
add_action( 'save_post', 'custom_save_post_meta' );

/* Adds a box to the main column on the Post and Page edit screens */
function my_meta_box() {
add_meta_box( 'wp_editor_metabox', 'Metabox with Editor', 'wp_editor_meta_box' );
}

/* Prints the box content */
function wp_editor_meta_box( $post ) {

// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'metabox_nonce' );

$field_value = get_post_meta( $post->ID, 'custom_meta', true );
//Add a resizable editor, with a minimum height of 100 px
$args = array(
'tinymce' => array(
'autoresize_min_height' => 100,
'wp_autoresize_on' => true,
'plugins' => 'wpautoresize',
'toolbar1' => 'bold,italic,underline,link,unlink,forecolor',
'toolbar2' => '',
),
);
wp_editor( $field_value, 'custom_meta', $args );
}

/* When the post is saved, saves our custom data */
function custom_save_post_meta( $post_id ) {

// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( ( isset ( $_POST['metabox_nonce'] ) ) && ( ! wp_verify_nonce( $_POST['metabox_nonce'], plugin_basename( __FILE__ ) ) ) )
return;

// Check permissions
if ( ( isset ( $_POST['post_type'] ) ) && ( 'page' == $_POST['post_type'] ) ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}

// OK, we're authenticated: we need to find and save the data
if ( isset ( $_POST['custom_meta'] ) ) {
update_post_meta( $post_id, 'custom_meta', $_POST['custom_meta'] );
}

}

WSE Answer related to this: https://wordpress.stackexchange.com/questions/284112/auto-resize-when-writing-with-custom-post-wp-editor/284465#284465

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.