Answers:
要将按钮添加到TinyMCE编辑器,我们需要做几件事:
在这些步骤中,我们注册了TinyMCE插件,该插件将位于javascript文件中'path/to/shortcode.js'
(请参见wpse72394_register_tinymce_plugin()
下文)
// init process for registering our button
add_action('init', 'wpse72394_shortcode_button_init');
function wpse72394_shortcode_button_init() {
//Abort early if the user will never see TinyMCE
if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') && get_user_option('rich_editing') == 'true')
return;
//Add a callback to regiser our tinymce plugin
add_filter("mce_external_plugins", "wpse72394_register_tinymce_plugin");
// Add a callback to add our button to the TinyMCE toolbar
add_filter('mce_buttons', 'wpse72394_add_tinymce_button');
}
//This callback registers our plug-in
function wpse72394_register_tinymce_plugin($plugin_array) {
$plugin_array['wpse72394_button'] = 'path/to/shortcode.js';
return $plugin_array;
}
//This callback adds our button to the toolbar
function wpse72394_add_tinymce_button($buttons) {
//Add the button ID to the $button array
$buttons[] = "wpse72394_button";
return $buttons;
}
现在我们需要创建我们的TinyMCE插件。这将放入文件中'path/to/shortcode.js'
(如早期步骤中所指定)。
jQuery(document).ready(function($) {
tinymce.create('tinymce.plugins.wpse72394_plugin', {
init : function(ed, url) {
// Register command for when button is clicked
ed.addCommand('wpse72394_insert_shortcode', function() {
selected = tinyMCE.activeEditor.selection.getContent();
if( selected ){
//If text is selected when button is clicked
//Wrap shortcode around it.
content = '[shortcode]'+selected+'[/shortcode]';
}else{
content = '[shortcode]';
}
tinymce.execCommand('mceInsertContent', false, content);
});
// Register buttons - trigger above command when clicked
ed.addButton('wpse72394_button', {title : 'Insert shortcode', cmd : 'wpse72394_insert_shortcode', image: url + '/path/to/image.png' });
},
});
// Register our TinyMCE plugin
// first parameter is the button ID1
// second parameter must match the first parameter of the tinymce.create() function above
tinymce.PluginManager.add('wpse72394_button', tinymce.plugins.wpse72394_plugin);
});
init
钩子更改为钩子admin_init
还可以节省前端的一些额外处理。
admin_init
则更可取。
这里有太多内容无法完整回答,因此请查看此指南:http : //wp.smashingmagazine.com/2012/05/01/wordpress-shortcodes-complete-guide/
您必须创建一个Javascript文件,该文件对通过WordPress注册的按钮执行操作,然后将TinyMCE按钮插入编辑器。