Skip to content

Instantly share code, notes, and snippets.

@FinnWoelm
Last active August 1, 2016 20:08
Show Gist options
  • Save FinnWoelm/78031e6287a0c8fe587cc869dff24162 to your computer and use it in GitHub Desktop.
Save FinnWoelm/78031e6287a0c8fe587cc869dff24162 to your computer and use it in GitHub Desktop.
Adding button to Wordpress TinyMCE Editor via Theme Files
<?php
// functions.php
// ...
// add new buttons
add_filter( 'mce_buttons', 'myplugin_register_buttons' );
function myplugin_register_buttons( $buttons ) {
array_push( $buttons, '|', 'custom_class');
return $buttons;
}
// Load the TinyMCE plugin : editor_plugin.js (wp2.5)
add_filter( 'mce_external_plugins', 'myplugin_register_tinymce_javascript' );
function myplugin_register_tinymce_javascript( $plugin_array ) {
$plugin_array['myplugin'] = get_stylesheet_directory_uri() . '/tinymce-plugin.js';
return $plugin_array;
}
// ...
?>
// tinymce-plugin.js
(function() {
tinymce.PluginManager.add( 'myplugin', function( editor, url ) {
// Add Button to Visual Editor Toolbar
editor.addButton('custom_class', {
title: 'Insert CSS Class',
cmd: 'custom_class',
image: url + '/icon.png',
});
// Add Command when Button Clicked
editor.addCommand('custom_class', function() {
alert('Button clicked!');
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment