mce_external_plugins
云策文档标注
概述
mce_external_plugins 是一个 WordPress 过滤器,用于管理 TinyMCE 编辑器的外部插件列表。它允许开发者通过关联数组添加自定义或核心插件,并支持指定编辑器 ID 以针对不同编辑器实例进行配置。
关键要点
- 过滤器接收一个关联数组,格式为 'plugin_name' => 'url',其中 URL 必须是绝对路径并包含要加载的 JS 文件名。
- 如果外部插件添加按钮,应使用 'mce_buttons' 过滤器进行注册。
- 参数包括 $external_plugins(插件数组)和 $editor_id(编辑器标识符,如 'content' 或 'classic-block')。
- 插件必须与 WordPress 中集成的 TinyMCE 版本兼容,注意版本更新(如 WordPress 3.9 升级到 TinyMCE 4.0)。
- 避免与 'tiny_mce_plugins' 过滤器混淆,否则脚本可能无法正确注册。
代码示例
/**
* Add the TinyMCE VisualBlocks Plugin.
*
* @param array $plugins An array of all plugins.
* @return array
*/
function my_custom_plugins( $plugins ) {
$plugins['visualblocks'] = plugins_url( 'tinymce/', __FILE__ ) . 'visualblocks/editor_plugin.js';
return $plugins;
}
add_filter( 'mce_external_plugins', 'my_custom_plugins' );注意事项
- 确保插件脚本路径正确,并使用绝对 URL。
- 在添加插件时,同时使用 'mce_buttons' 过滤器来注册按钮(如果插件包含按钮功能)。
- 注意编辑器 ID 参数的使用,以支持多编辑器场景。
原文内容
Filters the list of TinyMCE external plugins.
Description
The filter takes an associative array of external plugins for TinyMCE in the form ‘plugin_name’ => ‘url’.
The url should be absolute, and should include the js filename to be loaded. For example: ‘myplugin’ => ‘http://mysite.com/wp-content/plugins/myfolder/mce_plugin.js‘.
If the external plugin adds a button, it should be added with one of the ‘mce_buttons’ filters.
Parameters
$external_pluginsarray-
An array of external TinyMCE plugins.
$editor_idstring-
Unique editor identifier, e.g.
'content'. Accepts'classic-block'when called from block editor’s Classic block.
Source
$mce_external_plugins = apply_filters( 'mce_external_plugins', array(), $editor_id );
Skip to note 3 content
Steven Lin
Example migrated from Codex:
Visualblock plugin
For example, you might create a WordPress plugin that loads the TinyMCE plugin visualblocks.
TinyMCE plugins typically consist of a javascript file named ‘editor_plugin.js’ and a series of CSS files and other helper files. This example assumes the WordPress plugin stores TinyMCE plugins as follows: example_plugin/tinymce/visualblocks/edior_plugin.js
/** * Add the TinyMCE VisualBlocks Plugin. * * @param array $plugins An array of all plugins. * @return array */ function my_custom_plugins( $plugins ) { $plugins['visualblocks'] = plugins_url( 'tinymce/', __FILE__ ) . 'visualblocks/editor_plugin.js'; return $plugins; } add_filter( 'mce_external_plugins', 'my_custom_plugins' );Skip to note 4 content
Steven Lin
Example migrated from Codex:
Creating a shortcode and a tag button
First, create a plugin that registers the buttons and their JavaScript.
/* Plugin Name: My TinyMCE Buttons */ add_action( 'admin_init', 'my_tinymce_button' ); function my_tinymce_button() { if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) ) { add_filter( 'mce_buttons', 'my_register_tinymce_button' ); add_filter( 'mce_external_plugins', 'my_add_tinymce_button' ); } } function my_register_tinymce_button( $buttons ) { array_push( $buttons, "button_eek", "button_green" ); return $buttons; } function my_add_tinymce_button( $plugin_array ) { $plugin_array['my_button_script'] = plugins_url( '/mybuttons.js', __FILE__ ) ; return $plugin_array; }Then, in the same folder as the plugin, the JavaScript file mybuttons.js. We add our two buttons (eek and green), one is a direct
onclickthat inserts a Shortcode and the other is a command that adds anh3tag to the selected text in the editor.(function() { /* Register the buttons */ tinymce.create('tinymce.plugins.MyButtons', { init : function(ed, url) { /** * Inserts shortcode content */ ed.addButton( 'button_eek', { title : 'Insert shortcode', image : '../wp-includes/images/smilies/icon_eek.gif', onclick : function() { ed.selection.setContent('[myshortcode]'); } }); /** * Adds HTML tag to selected content */ ed.addButton( 'button_green', { title : 'Add span', image : '../wp-includes/images/smilies/icon_mrgreen.gif', cmd: 'button_green_cmd' }); ed.addCommand( 'button_green_cmd', function() { var selected_text = ed.selection.getContent(); var return_text = ''; return_text = '<h1>' + selected_text + '</h1>'; ed.execCommand('mceInsertContent', 0, return_text); }); }, createControl : function(n, cm) { return null; }, }); /* Start the buttons */ tinymce.PluginManager.add( 'my_button_script', tinymce.plugins.MyButtons ); })();You can pass PHP values to the JavaScript file printing directly in
admin_head:foreach ( array('post.php','post-new.php') as $hook ) {.add_action( "admin_head-$hook", 'my_admin_head' );
}
/**
* Localize Script
*/
function my_admin_head() {
$plugin_url = plugins_url( '/', __FILE__ );
?>
<!-- TinyMCE Shortcode Plugin -->
<script type='text/javascript'>
var my_plugin = {
'url': '<?php echo $plugin_url; ?>',
};
</script>
<!-- TinyMCE Shortcode Plugin -->
</pre>
<p>And then, access it in <strong>mybuttons.js</strong> with <code>console.log(my_plugin.url);