Created
April 15, 2020 15:12
-
-
Save nfsarmento/08f1ca62357df508b2e5d00daf024021 to your computer and use it in GitHub Desktop.
Replaces the default excerpt editor with TinyMCE.
This file contains hidden or 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
| /** | |
| * Replaces the default excerpt editor with TinyMCE. | |
| */ | |
| class NS_Richtext_Excerpt | |
| { | |
| /** | |
| * Replaces the meta boxes. | |
| * | |
| * @return void | |
| */ | |
| public static function switch_boxes() | |
| { | |
| if ( ! post_type_supports( $GLOBALS['post']->post_type, 'excerpt' ) ) | |
| { | |
| return; | |
| } | |
| remove_meta_box( | |
| 'postexcerpt' // ID | |
| , '' // Screen, empty to support all post types | |
| , 'normal' // Context | |
| ); | |
| add_meta_box( | |
| 'postexcerpt2' // Reusing just 'postexcerpt' doesn't work. | |
| , __( 'Excerpt' ) // Title | |
| , array ( __CLASS__, 'show' ) // Display function | |
| , null // Screen, we use all screens with meta boxes. | |
| , 'normal' // Context | |
| , 'core' // Priority | |
| ); | |
| } | |
| /** | |
| * Output for the meta box. | |
| * | |
| * @param object $post | |
| * @return void | |
| */ | |
| public static function show( $post ) | |
| { | |
| ?> | |
| <label class="screen-reader-text" for="excerpt"><?php | |
| _e( 'Excerpt' ) | |
| ?></label> | |
| <?php | |
| // We use the default name, 'excerpt', so we don’t have to care about | |
| // saving, other filters etc. | |
| wp_editor( | |
| self::unescape( $post->post_excerpt ), | |
| 'excerpt', | |
| array ( | |
| 'textarea_rows' => 15 | |
| , 'media_buttons' => FALSE | |
| , 'teeny' => TRUE | |
| , 'tinymce' => TRUE | |
| ) | |
| ); | |
| } | |
| /** | |
| * The excerpt is escaped usually. This breaks the HTML editor. | |
| * | |
| * @param string $str | |
| * @return string | |
| */ | |
| public static function unescape( $str ) | |
| { | |
| return str_replace( | |
| array ( '<', '>', '"', '&', ' ', '&nbsp;' ) | |
| , array ( '<', '>', '"', '&', ' ', ' ' ) | |
| , $str | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment