简体   繁体   中英

Limit WooCommerce Product Short Description to text only (Disable tinymce editor)

I want to limit the Product Short Description, so that only text can be added. (Disable tinymce editor). As there is post excerpt in regular posts.

Current:

  • Product Short Description = TinyMCE Editor

产品简短描述 = TinyMCE 编辑器

Expected:

  • Regular Posts Excerpt = TEXT ONLY

常规帖子摘录 = 仅文本


From what i found, this is what WooCommerce does to replace the standard excerpt box.

In includes/admin/class-wc-admin-meta-boxes.php on line 119 there is

add_meta_box( 'postexcerpt', __( 'Product short description', 'woocommerce' ), 'WC_Meta_Box_Product_Short_Description::output', 'product', 'normal' );

What refers to includes/admin/meta-boxes/class-wc-meta-box-product-short-description.php

/**
 * WC_Meta_Box_Product_Short_Description Class.
 */
class WC_Meta_Box_Product_Short_Description {

    /**
     * Output the metabox.
     *
     * @param WP_Post $post Post object.
     */
    public static function output( $post ) {

        $settings = array(
            'textarea_name' => 'excerpt',
            'quicktags'     => array( 'buttons' => 'em,strong,link' ),
            'tinymce'       => array(
                'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
                'theme_advanced_buttons2' => '',
            ),
            'editor_css'    => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
        );

        wp_editor( htmlspecialchars_decode( $post->post_excerpt, ENT_QUOTES ), 'excerpt', apply_filters( 'woocommerce_product_short_description_editor_settings', $settings ) );
    }
}

Any advice would be appreciated

You can use the wp_editor_settings WordPress hook in combination with some conditions.

Note that not only 'tinymce' is set to false, but also 'quicktags' and 'mediabuttons'

So you get:

function filter_wp_editor_settings( $settings, $editor_id ) {
    global $post;
    
    // Target
    if ( $editor_id == 'excerpt' && get_post_type( $post ) == 'product' ) {
        // Settings
        $settings = array(
            // Disable autop if the current post has blocks in it.
            'wpautop'             => ! has_blocks(),
            'media_buttons'       => false,
            'default_editor'      => '',
            'drag_drop_upload'    => false,
            'textarea_name'       => $editor_id,
            'textarea_rows'       => 20,
            'tabindex'            => '',
            'tabfocus_elements'   => ':prev,:next',
            'editor_css'          => '',
            'editor_class'        => '',
            'teeny'               => false,
            '_content_editor_dfw' => false,
            'tinymce'             => false,
            'quicktags'           => false,
        );
    }
    
    return $settings;
}
add_filter( 'wp_editor_settings', 'filter_wp_editor_settings', 1, 2 );

To avoid you can still add/write html tags, use:

Credits: A BM

function filter_woocommerce_short_description( $short_description ) { 
    return wp_strip_all_tags( $short_description );
} 
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM