Skip to content

Instantly share code, notes, and snippets.

@setola
Created August 2, 2012 10:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save setola/3236095 to your computer and use it in GitHub Desktop.
Save setola/3236095 to your computer and use it in GitHub Desktop.
Wordpress - excerpt with wysiwyg editor
<?php
/**
* This class removes the default excerpt metabox
* and adds a new box with the wysiwyg editor capability
* @author etessore
*/
class TinyMceExcerptCustomization{
const textdomain = '';
const custom_exceprt_slug = '_custom-excerpt';
var $contexts;
/**
* Set the feature up
* @param array $contexts a list of context where you want the wysiwyg editor in the excerpt field. Defatul array('post','page')
*/
function __construct($contexts=array('post', 'page')){
$this->contexts = $contexts;
add_action('admin_menu', array($this, 'remove_excerpt_metabox'));
add_action('add_meta_boxes', array($this, 'add_tinymce_to_excerpt_metabox'));
add_filter('wp_trim_excerpt', array($this, 'custom_trim_excerpt'), 10, 2);
add_action('save_post', array($this, 'save_box'));
}
/**
* Removes the default editor for the excerpt
*/
function remove_excerpt_metabox(){
foreach($this->contexts as $context)
remove_meta_box('postexcerpt', $context, 'normal');
}
/**
* Adds a new excerpt editor with the wysiwyg editor
*/
function add_tinymce_to_excerpt_metabox(){
foreach($this->contexts as $context)
add_meta_box(
'tinymce-excerpt',
__('Excerpt', self::textdomain),
array($this, 'tinymce_excerpt_box'),
$context,
'normal',
'high'
);
}
/**
* Manages the excerpt escaping process
* @param string $text the default filtered version
* @param string $raw_excerpt the raw version
*/
function custom_trim_excerpt($text, $raw_excerpt) {
global $post;
$custom_excerpt = get_post_meta($post->ID, self::custom_exceprt_slug, true);
if(empty($custom_excerpt)) return $text;
return $custom_excerpt;
}
/**
* Prints the markup for the tinymce excerpt box
* @param object $post the post object
*/
function tinymce_excerpt_box($post){
$content = get_post_meta($post->ID, self::custom_exceprt_slug, true);
if(empty($content)) $content = '';
wp_editor(
$content,
self::custom_exceprt_slug,
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_rows' => 10,
'textarea_name' => self::custom_exceprt_slug
)
);
}
/**
* Called when the post is beeing saved
* @param int $post_id the post id
*/
function save_box($post_id){
update_post_meta($post_id, self::custom_exceprt_slug, $_POST[self::custom_exceprt_slug]);
}
}
global $tinymce_excerpt;
$tinymce_excerpt = new TinyMceExcerptCustomization();
@davidbenton
Copy link

Thanks for this. It saved me some time while working on a recent client site. I've forked your gist with my changes. I fixed an autosave bug that was overwriting custom excerpts with an empty string and enabled media buttons to match the default TinyMCE editor.

With your permission, I'd like to submit it to the Wordpress plugin directory, as I think it would be valuable for others. If I did so, I was wondering what you'd like me to use as the plugin URL and author name and URL.

Cheers,
David

@alexlino
Copy link

Thank you very much. As davidbenton mentioned before, i found an issue with autosave. When wordpress autosaves the post, the excerpt goes away. I solved by disabling wp autosave. Another option is to set the autosave time in wp-config. Nice work. Any chances to have a character counter on this?

@eddypareja
Copy link

Great code Setola! I've been using it for a while but just noticed that the excerpt is overwritten with blank content when you use the "Quick Edit", as well.. could this be cause by the same autosave bug you guys mentioned? If so, did you ever make a plugin with that fix, David?

@junias91
Copy link

junias91 commented Sep 3, 2019

When I create a new post I get the following error message: Undefined index: _custom-excerpt
How can I fix this?


Wenn ich einen neuen Post erstelle bekomme ich folgende Fehlermeldung: Undefined index: _custom-excerpt
Wie kann ich das fixen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment