我正试图清理WordPress中粘贴的文本,因此我决定按照this tutorial.
添加一个过滤器似乎很简单before_wp_tiny_mce
并添加一个设置来清理文本。
我的问题是tiny_mce_before_init
从未触发。我可以看到这些挂钩触发:before_wp_tiny_mce
, wp_tiny_mce_init
, after_wp_tiny_mce
但从来没有tiny_mce_before_init
.
我到处都找遍了,没有任何东西拆下过滤器,也没有任何扩展来更换tinymce。
如果有人有主意?
编辑,下面是代码(我试着将其放在我的子主题的function.php中,其他钩子在那里工作):
add_filter(\'tiny_mce_before_init\',\'configure_tinymce\');
/**
* Customize TinyMCE\'s configuration
*
* @param array
* @return array
*/
function configure_tinymce($in) {
$in[\'paste_preprocess\'] = "function(plugin, args){
// Strip all HTML tags except those we have whitelisted
var whitelist = \'p,span,b,strong,i,em,h3,h4,h5,h6,ul,li,ol\';
var stripped = jQuery(\'<div>\' + args.content + \'</div>\');
var els = stripped.find(\'*\').not(whitelist);
for (var i = els.length - 1; i >= 0; i--) {
var e = els[i];
jQuery(e).replaceWith(e.innerHTML);
}
// Strip all class and id attributes
stripped.find(\'*\').removeAttr(\'id\').removeAttr(\'class\');
// Return the clean HTML
args.content = stripped.html();
}";
return $in;
}
最合适的回答,由SO网友:phatskat 整理而成
查看class-wp-editor.php
, 看起来像是tiny_mce_before
仅在未使用小版本编辑器时触发:
/*
* 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\'] ) {
/**
* Filters 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 {
/**
* Filters 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 );
}
如果配置可以同时适用于Tiny和Teeny,那么可以挂接这两个过滤器来修改配置。