我已经按照 Nettuts 上的教程了解如何向 TinyMCE 添加自定义按钮(http://net.tutsplus.com/tutorials/wordpress/wordpress-shortcodes-the-right-way/)
它工作得很好,但我想添加许多按钮,我想知道是否有一种聪明的方法可以做到这一点,而不必一遍又一遍地复制所有代码。
这是我用于添加按钮的代码:
add_shortcode("quote", "quote");
function quote( $atts, $content = null ) {
return '<div class="right text">"'.$content.'"</div>';
}
add_action('init', 'add_button');
function add_button() {
if ( current_user_can('edit_posts') && current_user_can('edit_pages') )
{
add_filter('mce_external_plugins', 'add_plugin');
add_filter('mce_buttons_3', 'register_button');
}
}
function register_button($buttons) {
array_push($buttons, "quote");
return $buttons;
}
function add_plugin($plugin_array) {
$plugin_array['quote'] = get_bloginfo('template_url').'/js/customcodes.js';
return $plugin_array;
}
然后我使用以下代码创建一个 customcodes.js 文件:
(function() {
tinymce.create('tinymce.plugins.quote', {
init : function(ed, url) {
ed.addButton('quote', {
title : 'Add a Quote',
image : url+'/image.png',
onclick : function() {
ed.selection.setContent('[quote]' + ed.selection.getContent() + '[/quote]');
}
});
},
createControl : function(n, cm) {
return null;
},
});
tinymce.PluginManager.add('quote', tinymce.plugins.quote);
})();
再说一遍,我怎样才能添加多个按钮而不必为每个新按钮执行所有这些代码?
谢谢 :) 塞巴斯蒂安