Created
August 2, 2012 10:17
-
-
Save setola/3236095 to your computer and use it in GitHub Desktop.
Wordpress - excerpt with wysiwyg editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
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?
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
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?