php - TinyMCE 中的自定义格式 div 与以前的 div 合并

标签 php wordpress tinymce tinymce-4

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

array(
    'title' => 'Tab',
    'block' => 'div',
    'classes' => 'tab',
    'wrapper' => true,
    'exact' => true,
),

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

<div class="tab">
   <h3>My Content heading 1</h3>
   <p>My first bit of content here...</p>
</div>

到目前为止,还不错。但是,当我选择以下文本并选择我的格式时,它会扩展现有的 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>

当我想要的是:

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

在创建格式时设置“精确”参数似乎并没有像我期望的那样改变这种行为。请帮助我。

最佳答案

将这些函数添加到主题的“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;
    }
}

然后在你的'[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;
        });
    });
})();

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

#tinymce .tab{ padding:10px; border: 1px #666 solid; }

希望这对您有所帮助,并归功于 https://madebydenis.com/adding-custom-buttons-in-tinymce-editor-in-wordpress/ .

关于php - TinyMCE 中的自定义格式 div 与以前的 div 合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46019046/

相关文章:

php - 未设置 Redhat Openshift MySQL 环境变量

mysql - 使用 lib_mysqludf_preg 库替换每个帖子的第一张图片

jquery - TinyMCE 4 - 删除()或销毁()

php - 在 Woocommerce 3 中以编程方式设置自定义运费

javascript - 如何针对 TinyMCE 编辑器进行样式设置

jquery - 单击按钮后,tinyMCE 光标位于文本中间

PhpStorm + Xdebug = 页面未加载

PHP:为什么这些输出不同的结果?

php - 从mysql数据库拉取数据之前确保wamp服务器在线 -Android

wordpress - 如何使用子目录在 Azure 上设置 Wordpress 多站点