停止TinyMCE在WordPress中剥离空标签 - 是的,我已经研究过了


Stop TinyMCE stripping empty tags in WordPress - yes I have researched already

像许多其他人一样,我对TinyMCE剥离HTML标签有问题 - 特别是我与Font Awesome一起使用的空标签。

我已经研究并尝试了解决方案,但没有任何效果。 我对PHP不是特别强,但我遇到的问题是:每个人都说修改tinymce.js文件中的tinyMCE.init函数。 但是在最新版本的WP中,我没有。 我有类-wp-editor.php,其中生活是这样的:

            /*
         * For people who really REALLY know what they're doing with TinyMCE
         * You can modify $mceInit to add, remove, change elements of the config
         * before tinyMCE.init. Setting "valid_elements", "invalid_elements"
         * and "extended_valid_elements" can be done through this filter. Best
         * is to use the default cleanup by not specifying valid_elements,
         * as TinyMCE checks against the full set of HTML 5.0 elements and attributes.
         */
        if ( $set['teeny'] ) {
            /**
             * Filter the teenyMCE config before init.
             *
             * @since 2.7.0
             *
             * @param array  $mceInit   An array with teenyMCE config.
             * @param string $editor_id Unique editor identifier, e.g. 'content'.
             */
            $mceInit = apply_filters( 'teeny_mce_before_init', $mceInit, $editor_id );
        } else {
            /**
             * Filter the TinyMCE config before init.
             *
             * @since 2.5.0
             *
             * @param array  $mceInit   An array with TinyMCE config.
             * @param string $editor_id Unique editor identifier, e.g. 'content'.
             */
            $mceInit = apply_filters( 'tiny_mce_before_init', $mceInit, $editor_id );
        }

现在我知道我必须对valid_elements、extended_valid_elements或verify_html做点什么,但我不知道该怎么做。 我可以从注释中看到放置它的位置,但我不知道该使用哪个或正确的语法。

我发现了这个小提琴:http://fiddle.tinymce.com/j9baab/1 但就像我说的,我在 WP 中的任何地方都没有该功能。

请帮忙!

你真的不想修改class-wp-editor.php文件,因为每次更新WordPress时它都会被覆盖。

扩展/修改TinyMCE设置的最简单方法是构建一个简单的WordPress插件,并绑定到WordPress提供的钩子中以更改编辑器的设置。

解决您在要查看的初始化中添加选项的特殊愿望'tiny_mce_before_init'钩。 您可以在插件中执行以下操作:

add_filter('tiny_mce_before_init', 'add_my_options');
function add_my_options($opt) {   
    // $opt is the existing array of options for TinyMCE 
    // We simply add a new array element where the name is the name
    // of the TinyMCE configuration setting.  The value of the array
    // object is the value to be used in the TinyMCE config.
    $opt['extended_valid_elements'] = '*[*]';
    return $opt;
}

编写一个简单的WP插件并不太难 - 网络上有很多例子。 对于这么简单的东西,它实际上只是一个PHP文件。 构建插件后,您只需安装并激活它即可。 激活后,每次调用TinyMCE时都会运行您的代码,并将您的选项注入到初始化代码中。

编辑:这是来自OPs注释的代码(比注释的格式更容易阅读(:

<?php
/**
 * Plugin Name: Disable TinyMCE Filters
 * Plugin URI: http://mindspyder.com
 * Description: This plugin disables annoying TinyMCE Filters.
 * Version: 1.0.0
 * Author: Brandon Snow
 * Author URI: http://mindspyder.com
 * License: GPL2
 */
 add_filter('tiny_mce_before_init', 'add_my_options');
function add_my_options($opt) {   
    // $opt is the existing array of options for TinyMCE 
    // We simply add a new array element where the name is the name
    // of the TinyMCE configuration setting.  The value of the array
    // object is the value to be used in the TinyMCE config.
    $opt['extended_valid_elements'] = '*[*]';
    return $opt;
}
?>