Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Created October 31, 2017 08:36
Show Gist options
  • Save UmeshSingla/d9079e3472c5f893e237e05af4c47a21 to your computer and use it in GitHub Desktop.
Save UmeshSingla/d9079e3472c5f893e237e05af4c47a21 to your computer and use it in GitHub Desktop.
Allows to add auto resizing to a custom wp_editor() instance added in a metabox.
<?php
/* 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'] );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment