Once in a while it might happen that you will need to add custom button to the default tinyMCE editor bundled with WordPress to enchance it’s functionality. Fortunately, WordPress have apprioprate hooks that allow to do this task with ease.
If you do not have time to read the whole article scroll down to the bottom and download completed plugin.
Before we start
In this tutorial i will assume you know the basics about creating WordPress plugins. If not then please read some intruductionary article before going further.
The plugin we are going to create will have a code name TinyPlugin, let’s put it in tiny-plugin directory (inside wp-content/plugins directory). In the main plugin file we are going to use two filters only.
Adding WordPress tinyMCE filters
add_filter('mce_external_plugins', "tinyplugin_register"); add_filter('mce_buttons', 'tinyplugin_add_button', 0);
The first one (mce_external_plugins) register the tinyMCE plugin, second (mce_buttons) is responsible for adding a button tinyMCE tooltip. The callbacks are as follow.
function tinyplugin_add_button($buttons) { array_push($buttons, "separator", "tinyplugin"); return $buttons; } function tinyplugin_register($plugin_array) { $url = trim(get_bloginfo('url'), "/"; $url.= "/wp-content/plugins/tiny-plugin/editor_plugin.js"; $plugin_array["tinyplugin"] = $url; return $plugin_array; }
The tinyplugin_add_button() function adds “separator” and “tinyplugin” to the buttons array. Actually separator is not required, it is just used to separate default buttons from our custom one, on the other hand “tinyplugin” is the name of our plugin which we are going to create in JavaScript.
While tinyplugin_add_button() adds button to the toolbar, it’s tinyplugin_register() function that actually registers the plugin. Now we need to create editor_plugin.js JavaScript that will contain tinyMCE plugin code.
Creating tinyMCE plugin
From the tinyplugin_register() function you might already guessed that we need to create editor_plugin.js file in tiny-plugin directory.
Create it and add to it:
function tiny_plugin() { return "[tiny-plugin]"; } (function() { tinymce.create('tinymce.plugins.tinyplugin', { init : function(ed, url){ ed.addButton('tinyplugin', { title : 'Insert TinyPlugin', onclick : function() { ed.execCommand( 'mceInsertContent', false, tiny_plugin() ); }, image: url + "/wand.png" }); } }); tinymce.PluginManager.add('tinyplugin', tinymce.plugins.tinyplugin); })();
This code snippet also has two functions (one named and one anonymous). tiny_plugin() function returns content that will be inserted into post after clicking the button. Second anonymous function registers plugin with tinyMCE.





12 Comments on "Adding custom buttons to WordPress tinyMCE"
useful info. But I am still struggling to make a button which decreases the size of the text selected.
Yes please do a followup with more info on how to create a wizard and ask for optional parameters etc
Please?
I cannot get this to work.
@george do you mean the plugin in a zip file i included at the bottom?
Awesome! Thanks so heaps, I have been searching for ages for a really simple example of this to get me started and there aren’t many out there. Cheers
For anyone copying the code above, there is a missing closing parenthesis on line 9 of the 2nd PHP section.
$url = trim(get_bloginfo(‘url’), “/”;
Should be
$url = trim(get_bloginfo(‘url’), “/”);
Thanks for the tutorial!
Excellent article!
Nice article. You know how make it with “selects”, like “Paragraph”?
Thanks.
@Andre when i will find some time i will update the article with info how to add “select” button.
Would love to see some example code with multiple buttons registered..
I cannot get this to work.
Excellent article! i try and works
thks