将 WooCommerce 产品简短描述限制为仅文本(禁用 tinymce 编辑器)

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

我想限制产品简短描述,以便只能添加文本。 (禁用 tinymce 编辑器)。因为在常规 post 中有 post 摘录。

当前:

预期:


根据我的发现,这就是 WooCommerce 用来替换标准摘录框的方法。

在第119行的includes/admin/class-wc-admin-meta-boxes.php中有

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

指的是什么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 ) );
    }
}

如有任何建议,我们将不胜感激

您可以结合某些条件使用 wp_editor_settings WordPress 挂钩。

注意不仅'tinymce'设置为false,'quicktags''mediabuttons'

也设置为false

所以你得到:

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 );

要避免您仍然可以 add/write html 标签,请使用:

学分: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 );