TinyMCE中的自定义格式div与之前的div合并

dar*_*k19 7 php wordpress tinymce tinymce-4

我在编辑器中创建了一个自定义格式

array(
    'title' => 'Tab',
    'block' => 'div',
    'classes' => 'tab',
    'wrapper' => true,
    'exact' => true,
),
Run Code Online (Sandbox Code Playgroud)

然后,我可以通过选择一些文本并选择我的格式来使用它:

<div class="tab">
   <h3>My Content heading 1</h3>
   <p>My first bit of content here...</p>
</div>
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.但是,当我选择以下文本并选择我的格式时,它会扩展现有的div而不是插入新的div,所以我最终得到:

<div class="tab">
  <h3>My Content heading 1</h3>
  <p>My first bit of content here...</p>
  <h3>My Content heading 2</h3>
  <p>My second bit of content here...</p>
</div>
Run Code Online (Sandbox Code Playgroud)

当我想要的是这个:

<div class="tab">
  <h3>My Content heading 1</h3>
  <p>My first bit of content here...</p>
</div>
<div class="tab">
  <h3>My Content heading 2</h3>
  <p>My second bit of content here...</p>
</div>
Run Code Online (Sandbox Code Playgroud)

在创建格式时设置"exact"参数似乎不会在我期望的时候改变这种行为.请帮帮我.

Out*_*ess 2

将这些函数添加到主题的“functions.php”中,以便在 TinyMCE 中注册新按钮。

// init scripts
add_action( 'after_setup_theme', 'custom_tinymce_theme_setup' );

if ( ! function_exists( 'custom_tinymce_theme_setup' ) ) {
    function custom_tinymce_theme_setup(){
        add_action( 'admin_init', 'custom_tinymce_theme_add_editor_styles' );
        add_action( 'init', 'custom_tinymce_buttons' );
    }
}

// add editor custom css
if ( ! function_exists( 'custom_tinymce_theme_add_editor_styles' ) ) {
    function custom_tinymce_theme_add_editor_styles() {
        add_editor_style( 'custom-editor-style.css' );
    }
}

// add editor button filter
if ( ! function_exists( 'custom_tinymce_buttons' ) ) {
    function custom_tinymce_buttons() {
        if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
            return;
        }
        if ( get_user_option( 'rich_editing' ) !== 'true' ) {
            return;
        }

        add_filter( 'mce_external_plugins', 'custom_tinymce_add_buttons' );
        add_filter( 'mce_buttons', 'custom_tinymce_register_buttons' );
    }
}

// call editor custom js
if ( ! function_exists( 'custom_tinymce_add_buttons' ) ) {
    function custom_tinymce_add_buttons( $plugin_array ) {
        $plugin_array['wrap_tab'] = get_template_directory_uri().'/js/tinymce_buttons.js';
        return $plugin_array;
    }
}

// add button to editor
if ( ! function_exists( 'custom_tinymce_register_buttons' ) ) {
    function custom_tinymce_register_buttons( $buttons ) {
        array_push( $buttons, 'wrap_tab' );
        return $buttons;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在“[theme]/js”目录下创建一个名为“tinymce_buttons.js”的新 JS 文件,并添加以下代码。

(function() {
    tinymce.PluginManager.add( 'wrap_tab', function( editor, url ) {
        editor.addButton('wrap_tab', {
            title: 'Insert Tab',
            cmd: 'wrap_tab',
            text: 'Tab',
        });

        editor.addCommand('wrap_tab', function() {
            var selected_text = editor.selection.getContent({
                'format': 'html'
            });
            if ( selected_text.length === 0 ) {
                alert( 'Please select text that needs to be wrapped.' );
                return;
            }
            var return_content = '<div class="tab">' + selected_text + '</div>';
            editor.execCommand('mceReplaceContent', false, return_content);
            return;
        });
    });
})();
Run Code Online (Sandbox Code Playgroud)

然后,您可以包含 CSS 样式 - 在主题文件夹下创建一个名为“custom-editor-style.css”的新 CSS 文件,其中包含 DIV 元素的样式定义。

#tinymce .tab{ padding:10px; border: 1px #666 solid; }
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助,并感谢https://madebydenis.com/adding-custom-buttons-in-tinymce-editor-in-wordpress/