WordPress Richtext Editor for Excerpts

Occasionally when creating a WordPress theme the design relies on using the excerpt meta box, but forcing the end user to manually type html tags defeats the entire purpose of using WordPress (or any CMS for that matter) and adds a layer of unnecessary complexity to editing posts, pages, or any other post type. To make things simpler you can replace the Excerpt meta box with a rich-text editor, just like the main content editor. This can be done by installing an additional plugin, however with just a couple extra lines of code it can also be done through your theme.

function lb_editor_remove_meta_box() {
	global $post_type;

	// Check to see if the global $post_type variable exists
	// and then check to see if the current post_type supports
	// excerpts. If so, remove the default excerpt meta box
	// provided by the WordPress core. If you would like to only
	// change the excerpt meta box for certain post types replace
	// $post_type with the post_type identifier.
	if (isset($post_type) && post_type_supports($post_type, 'excerpt')) remove_meta_box('postexcerpt', $post_type, 'normal');
}
add_action('admin_menu', 'lb_editor_remove_meta_box');

function lb_editor_add_custom_meta_box() {
	global $post_type;

	// Again, check to see if the global $post_type variable
	// exists and then if the current post_type supports excerpts.
	// If so, add the new custom excerpt meta box. If you would
	// like to only change the excerpt meta box for certain post
	// types replace $post_type with the post_type identifier.
	if (isset($post_type) && post_type_supports($post_type, 'excerpt')) add_meta_box('postexcerpt', __('Excerpt'), 'lb_editor_custom_post_excerpt_meta_box', $post_type, 'normal', 'high');
}
add_action( 'add_meta_boxes', 'lb_editor_add_custom_meta_box' );

function lb_editor_custom_post_excerpt_meta_box( $post ) {
	// Adjust the settings for the new wp_editor. For all
	// available settings view the wp_editor reference
	// http://codex.wordpress.org/Function_Reference/wp_editor
	$settings = array( 'textarea_rows' => '12', 'quicktags' => false, 'tinymce' => true);

	// Create the new meta box editor and decode the current
	// post_excerpt value so the TinyMCE editor can display
	// the content as it is styled.
	wp_editor(html_entity_decode(stripcslashes($post->post_excerpt)), 'excerpt', $settings);

	// The meta box description - adjust as necessary
	echo '<p><em>Excerpts are optional, hand-crafted, summaries of your content.</em></p>';
}

Thats it! The above code can be placed in the functions.php file of your theme or placed in its own plugin file. Note that typically with custom meta boxes additional code must be added to save the meta box value when the post/page is published or updated, however since the meta box is replacing the excerpt WordPress automatically handles this process.

Join the Conversation

4 Comments

Leave a comment

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

You can also subscribe without commenting.