Aggiunta di un rich text editor a Excerpt
4 risposta
- voti
-
- 2012-07-12
Basta sostituire l'outputpredefinito. Assicurati di annullare l'escape dell'estrattoprima diinviarlo all'editor:
add_action( 'add_meta_boxes', array ( 'T5_Richtext_Excerpt', 'switch_boxes' ) ); /** * Replaces the default excerpt editor with TinyMCE. */ class T5_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 ); } }
Salva questo codicein unplugin onelfile
functions.php
deltuotema.Just replace the default output. Make sure you unescape the excerpt before you send it to the editor:
add_action( 'add_meta_boxes', array ( 'T5_Richtext_Excerpt', 'switch_boxes' ) ); /** * Replaces the default excerpt editor with TinyMCE. */ class T5_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 ); } }
Save this code in a plugin or in your theme’s
functions.php
.-
Grazie,funziona ancorafuori dagli scheminel 2018per WP 4.9.x :)Thanks, still works out of the box in 2018 for WP 4.9.x :)
- 0
- 2018-06-13
- moped
-
Funzionaperfettamentenella versione 5.1 (febbraio 2019),grazie!Works perfectly in 5.1 version (Feb 2019), thanks!
- 0
- 2019-02-24
- Mayur Chauhan
-
Funziona ancoranel 2020,anche dopo Gutenberg (anche se l'hoprovato solo conilplug-in "Editor classico" abilitato)Still works in 2020, even post-Gutenberg (though I only tested it with "Classic Editor" plugin enabled)
- 0
- 2020-02-01
- squarecandy
-
- 2012-07-12
Unmodo semplice è utilizzareilplug-in Rich Text Excerpt
Ilplug-in utilizza lafunzione wp_editor pergenerare uneditor ditesto RTFperestratti dipagine/post,quindifunzionerà soloin WordPress 3.3 o versioni successive.
A simple way is to use the plugin Rich Text Excerpt
The Plugin uses the wp_editor function to generate a rich text editor for page/post excerpts, so will only work in WordPress 3.3 or greater.
-
Loprovo,ma èincompatibile conilplugin qTranslate,qualcheideaperfarlofunzionare?I try it, but it's incompatible with qTranslate plugin, any idea to make it work?
- 0
- 2012-07-12
- Marta
-
Loesaminerò.I will look into it.
- 0
- 2012-07-12
- Pontus Abrahamsson
-
Qualcheidea sulperché l'estrattonon sia una casella ditesto RTFperimpostazionepredefinita?Any idea why excerpt is not a rich text box by default?
- 0
- 2012-08-22
- urok93
-
- 2013-11-29
potresti dover utilizzare lafunzione
wp_editor
per ottenere un riccoeditor,quindi dovresti rimuoveretutte lefunzioni di sanitize conget_post_meta
(oupdate_post_meta
),allora dovresti usare lafunzionehtmlspecialchars_decode
per ottenereil ricco contenuto ..esamina questoprincipio:
add_action( 'add_meta_boxes', 'adding_a_new_metaabox' ); function adding_a_new_metaabox() { add_meta_box('html_myid_31_section', 'TITLE Hellooo', 'my_output_funct'); } function my_output_funct( $post ) { //so, dont ned to use esc_attr in front of get_post_meta $valueeee2= get_post_meta($_GET['post'], 'SMTH_METANAME' , true ) ; wp_editor( htmlspecialchars_decode($valueeee2), 'mettaabox_ID_stylee', $settings = array('textarea_name'=>'MyInputNAMEE') ); } function save_my_post_data( $post_id ) { if (!empty($_POST['MyInputNAMEE'])) { $datta=htmlspecialchars($_POST['MyInputNAMEE']); update_post_meta($post_id, 'SMTH_METANAME', $datta ); } } add_action( 'save_post', 'save_my_post_data' );
you may need to use
wp_editor
function to get rich editor, then you should remove any sanitize functions withget_post_meta
(orupdate_post_meta
), then you should usehtmlspecialchars_decode
function to get the rich content..look through this principle:
add_action( 'add_meta_boxes', 'adding_a_new_metaabox' ); function adding_a_new_metaabox() { add_meta_box('html_myid_31_section', 'TITLE Hellooo', 'my_output_funct'); } function my_output_funct( $post ) { //so, dont ned to use esc_attr in front of get_post_meta $valueeee2= get_post_meta($_GET['post'], 'SMTH_METANAME' , true ) ; wp_editor( htmlspecialchars_decode($valueeee2), 'mettaabox_ID_stylee', $settings = array('textarea_name'=>'MyInputNAMEE') ); } function save_my_post_data( $post_id ) { if (!empty($_POST['MyInputNAMEE'])) { $datta=htmlspecialchars($_POST['MyInputNAMEE']); update_post_meta($post_id, 'SMTH_METANAME', $datta ); } } add_action( 'save_post', 'save_my_post_data' );
-
Aggiungi una spiegazione allatua risposta: **perché ** questopotrebbe risolvereilproblema?Please add an explanation to your answer: **why** could that solve the problem?
- 0
- 2013-11-29
- fuxia
-
- 2015-09-30
Segui la soluzionee aggiungi l'estratto dell'editor wysiwyg subito dopoiltitolo delpost.
Aggiungi la classefollow altuoprogetto Wordpress come
excerpt.php
class Excerpt { public function __construct() { add_filter('excerpt_more', [$this, 'excerpt_more']); add_action('edit_form_after_title', [$this, 'excerpt']); add_action('admin_menu', [$this, 'remove_excerpt_metabox']); add_filter('wp_trim_excerpt', [$this, 'wp_trim_excerpt'], 10, 2); } /** * Remove metabox from post */ public function remove_excerpt_metabox() { remove_meta_box('postexcerpt', 'post', 'normal'); } /** * Strip tags * * @param string $text * @return string */ public function wp_trim_excerpt($text = '') { return strip_tags($text, '<a><strong><em><b><i><code><ul><ol><li><blockquote><del><ins><img><pre><code><>'); } /** * More sign... * * @return string */ public function excerpt_more() { return '…'; } /** * Excerpt editor after post title. * * @param $post */ public function excerpt($post) { if ($post->post_type !== 'post') return; wp_editor( html_entity_decode($post->post_excerpt), 'html-excerpt', [ 'teeny' => true, 'quicktags' => true, 'wpautop' => true, 'media_buttons' => false, 'textarea_rows' => 7, 'textarea_name' => 'excerpt' ] ); } }
Quindi aggiungi alfile
functions.php
seguendo le righe:require_once __DIR__ . '/excerpt.php'; $excerpt = new Excerpt();
Follow solution add excerpt wysiwyg editor right after post title.
Add follow class to your Wordpress project as
excerpt.php
class Excerpt { public function __construct() { add_filter('excerpt_more', [$this, 'excerpt_more']); add_action('edit_form_after_title', [$this, 'excerpt']); add_action('admin_menu', [$this, 'remove_excerpt_metabox']); add_filter('wp_trim_excerpt', [$this, 'wp_trim_excerpt'], 10, 2); } /** * Remove metabox from post */ public function remove_excerpt_metabox() { remove_meta_box('postexcerpt', 'post', 'normal'); } /** * Strip tags * * @param string $text * @return string */ public function wp_trim_excerpt($text = '') { return strip_tags($text, '<a><strong><em><b><i><code><ul><ol><li><blockquote><del><ins><img><pre><code><>'); } /** * More sign... * * @return string */ public function excerpt_more() { return '…'; } /** * Excerpt editor after post title. * * @param $post */ public function excerpt($post) { if ($post->post_type !== 'post') return; wp_editor( html_entity_decode($post->post_excerpt), 'html-excerpt', [ 'teeny' => true, 'quicktags' => true, 'wpautop' => true, 'media_buttons' => false, 'textarea_rows' => 7, 'textarea_name' => 'excerpt' ] ); } }
Then add to
functions.php
file follow lines:require_once __DIR__ . '/excerpt.php'; $excerpt = new Excerpt();
Devo aggiungere al campo dell'estratto l'editor di TinyMCE Advanced,qualcheidea?
Hoilplugin qTranslate (multilingua)ed èimpossibile collegare l'estratto con questoplugine uneditor.
Grazie