-
-
Save davidbenton/3658171 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 | |
/** | |
* @package wp-wysiwyg-excerpt | |
* This class removes the default excerpt metabox | |
* and adds a new box with the wysiwyg editor capability | |
* @author etessore | |
*/ | |
/* | |
Plugin Name: wp-wysiwyg-excerpt | |
Plugin URI: https://gist.github.com/3658171 | |
Description: Removes the default excerpt metabox and adds a new box with the wysiwyg editor capability. | |
Version: 0.1 | |
Author: Emanuele Tessore | |
Author URI: http://www.emanueletessore.com/ | |
*/ | |
/** | |
* 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_excerpt_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. Default 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_excerpt_slug, true); | |
if(empty($custom_excerpt)) return $text; | |
$custom_excerpt = do_shortcode($custom_excerpt); | |
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_excerpt_slug, true); | |
if(empty($content)) $content = ''; | |
wp_editor( | |
$content, | |
self::custom_excerpt_slug, | |
array( | |
'wpautop' => true, | |
'media_buttons' => true, | |
'textarea_rows' => 10, | |
'textarea_name' => self::custom_excerpt_slug | |
) | |
); | |
} | |
/** | |
* Called when the post is being saved | |
* @param int $post_id the post id | |
*/ | |
function save_box($post_id){ | |
if (isset($_POST[self::custom_excerpt_slug])) | |
update_post_meta($post_id, self::custom_excerpt_slug, $_POST[self::custom_excerpt_slug]); | |
} | |
} | |
global $tinymce_excerpt; | |
$tinymce_excerpt = new TinyMceExcerptCustomization(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment