我创建了自定义插件,在TinyMce编辑器中显示自定义按钮。使用经典的Wordpress编辑器工作正常,但按钮不会出现在acf WYSIWYG编辑器中。
这是我的代码:
add_filter('mce_external_plugins', 'tinyMceCustomShortcodesInit');
function tinyMceCustomShortcodesInit () {
$plugins = array('visualblocks');
$plugins_array = array();
foreach ($plugins as $plugin ) {
$plugins_array[ $plugin ] = plugins_url('tinymce/', __FILE__) . $plugin . '/editor_plugin.js';
}
return $plugins_array;
}
add_action( 'admin_init', 'tinyMceCustomShortcodesButton' );
function tinyMceCustomShortcodesButton() {
if ( current_user_can( 'edit_posts' ) && current_user_can( 'edit_pages' ) ) {
add_filter( 'mce_buttons', 'tinyMceCustomShortcodesRegisterButton' );
add_filter( 'mce_external_plugins', 'tinyMceCustomShortcodesAddButton' );
}
}
function tinyMceCustomShortcodesRegisterButton( $buttons ) {
array_push( $buttons, "button_eek", "button_green" );
return $buttons;
}
function tinyMceCustomShortcodesAddButton( $plugin_array ) {
$plugin_array['my_button_script'] = plugins_url( '/mybuttons.js', __FILE__ ) ;
return $plugin_array;
}
foreach ( array('post.php','post-new.php') as $hook ) {
add_action( "admin_head-$hook", 'tinyMceCustomShortcodesAdminHead' );
}
function tinyMceCustomShortcodesAdminHead() {
$plugin_url = plugins_url( '/', __FILE__ );
?>
<!-- TinyMCE Shortcode Plugin -->
<script type='text/javascript'>
var my_plugin = {
'url': '<?php echo $plugin_url; ?>',
};
</script>
<!-- TinyMCE Shortcode Plugin -->
<?php
}
我也创建了以下js文件: mybuttons.js
(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 );
})();
和tinymce / visualblocks / editor_plugin.js包含以下代码:
tinymce.init({
plugins: "visualblocks"
});
我遵循了这条准则:http://codex.wordpress.org/Plugin_API/Filter_Reference/mce_external_plugins