If you are WordPress developer, then at some point you may come across customizing or extending the WordPress Visual Editor. For example, you may want to add a button to the Visual Editorβs toolbar to allow your client to easily insert a text box or a call to action button without writing any HTML code. In this article, we will show you how to create a TinyMCE plugin in WordPress.
Requirements
This tutorial is intended for advanced users. If you are a beginner level user who just wants to extend visual editor, then please check out TinyMCE Advanced plugin or take a look at these tips on using WordPress visual editor.
For this tutorial, you will need basic coding skills, access to a WordPress install where you can test it out.
It is a bad practice to develop plugins on a live website. A minor mistake in the code can make your site inaccessible. But if you must do it on a live site, then at least backup WordPress first.
Creating Your First TinyMCE Plugin
We will begin by creating a WordPress plugin to register our custom TinyMCE toolbar button. When clicked, this button will allow user to add a link with a custom CSS class.
The source code will be provided in full at the end of this article, but until then, letβs create the plugin step-by-step.
First, you need to create a directory in wp-content/plugins folder of your WordPress install. Name this folder tinymce-custom-link-class.
From here, weβll begin adding our plugin code.
The Plugin Header
Create a new file in the plugin directory we just created and name this file tinymce-custom-link-class.php. Add this code to the file and save it.
* Plugin Name: TinyMCE Custom Link Class
* Plugin URI: http://pluginthemehub.com
* Version: 1.0
* Author: pluginthemehub
* Author URI: https://www.pluginthemehub.com
* Description: A simple TinyMCE Plugin to add a custom link class in the Visual Editor
* License: GPL2
*/
This is just a PHP comment, which tells WordPress the name of the plugin, as well as the author and a description.
In the WordPress admin area, activate your new plugin by going to Plugins > Installed Plugins, and then clicking Activate beside the TinyMCE Custom Link Class plugin:
Setting Up Our Plugin Class
If two WordPress plugins have functions with the same name, then this would cause an error. We will avoid this problem by having our functions wrapped in a class.
This creates our PHP class, along with a construct, which is called when we reach the line $tinymce_custom_link_class = new TinyMCE_Custom_Link_Class;.
Any functions we add inside this class shouldnβt conflict with other WordPress plugins.
Start Setting Up Our TinyMCE Plugin
Next, we need to tell TinyMCE that we might want to add our custom button to the Visual Editorβs toolbar. To do this, we can use WordPressβ actions β specifically, the init action.
Add the following code inside your pluginβs __construct() function:
add_action( ‘init’, array( $this, ‘setup_tinymce_plugin’ ) );
}
This checks if we are in the WordPress Administration interface. If we are, then it asks WordPress to run the setup_tinymce_plugin function inside our class when WordPress has finished its initial loading routine.
Next, add the setup_tinymce_plugin function:
This checks if the current logged in WordPress user can edit Posts or Pages. If they canβt, thereβs no point in registering our TinyMCE Plugin for that User, as theyβll never see the Visual Editor.
We then check if the user is using the Visual Editor, as some WordPress users turn this off via Users > Your Profile. Again, if the user is not using the Visual Editor, we return (exit) the function, as we donβt need to do anything else.
Finally, we add two WordPress Filters β mce_external_plugins and mce_buttons, to call our functions which will load the required Javascript file for TinyMCE, and add a button to the TinyMCE toolbar.
Registering the Javascript File and Button to the Visual Editor
Letβs go ahead and add the add_tinymce_plugin function:
This pushes two items onto the array of TinyMCE buttons: a separator (|), and our buttonβs programmatic name (custom_link_class).
Save your plugin, and then edit a Page or Post to view the Visual Editor. Chances are, the toolbar isnβt displaying right now:
Donβt worry β if we use our web browserβs inspector console, weβll see that a 404 error and notice have been generated by TinyMCE, telling us that it canβt find our Javascript file.
Thatβs good β it means weβve successfully registered our TinyMCE custom plugin, and now need to create the Javascript file to tell TinyMCE what to do.
Creating the Javascript Plugin
Create a new file in your wp-content/plugins/tinymce-custom-link-class folder, and name it tinymce-custom-link-class.js. Add this code in your js file:
tinymce.PluginManager.add( ‘custom_link_class’, function( editor, url ) {
});
})();
This calls the TinyMCE Plugin Manager class, which we can use to perform a number of TinyMCE plugin related actions. Specifically, weβre adding our plugin to TinyMCE using the add function.
This accepts two items; the name of the plugin (custom_link_class) and an anonymous function.
If youβre familiar with the concept of functions in coding, an anonymous function is simply a function with no name. For example, function foobar() { … } is a function that we could call somewhere else in our code by using foobar().
With an anonymous function, we canβt call that function somewhere else in our code β itβs only being called at the point the add() function is invoked.
Save your Javascript file, and then edit a Page or Post to view the Visual Editor. If everything worked, youβll see the toolbar:
Right now, our button hasnβt been added to that toolbar. Thatβs because weβve only told TinyMCE that we are a custom plugin. We now need to tell TinyMCE what to do β that is, add a button to the toolbar.
Update your Javascript file, replacing your existing code with the following:
tinymce.PluginManager.add( ‘custom_link_class’, function( editor, url ) {
// Add Button to Visual Editor Toolbar
editor.addButton(‘custom_link_class’, {
title: ‘Insert Button Link’,
cmd: ‘custom_link_class’,
});
});
})();
Notice our anonymous function has two arguments. The first is the editor instance β this is the TinyMCE Visual Editor. In the same way we can call various functions on the PluginManager, we can also call various functions on the editor. In this case, weβre calling the addButton function, to add a button to the toolbar.
Save your Javascript file, and go back to your Visual Editor. At a first look, nothing seems to have changed. However, if you hover your mouse cursor over to the right of the top rowβs rightmost icon, you should see a tooltip appear:
Reload our Visual Editor, and youβll now see your button with the icon:
Defining a Command to Run
Right now, if you click the button, nothing will happen. Letβs add a command to TinyMCE telling it what to do when our button is clicked.
In our Javascript file, add the following code below the end of the editor.addButton section:
editor.addCommand(‘custom_link_class’, function() {
alert(‘Button clicked!’);
});
Reload our Visual Editor, click the button and an alert will appear confirming we just clicked the button:
Letβs replace the alert with a prompt, asking the user for the link they want to wrap around the selected text in the Visual Editor:
This block of code performs a few actions.
First, we check if the user selected some text to be linked in the Visual Editor. If not, theyβll see an alert telling them to select some text to link.
Next, we ask them to enter a link, again checking if they did. If they cancelled, or didnβt enter anything, we donβt do anything else.
Summary
Weβve successfully created a WordPress plugin which adds a button to the TinyMCE visual editor in WordPress. This tutorial has also covered some of the basics of the TinyMCE API and WordPress filters available for TinyMCE integrations.
We added code so that when a user clicks our custom button, theyβre prompted to select some text in the Visual Editor, which they can then link to a URL of their choice. Finally, our plugin then replaces the selected text with a linked version that contains a custom CSS class called button.
We hope this tutorial helped you learn how to create a WordPress TinyMCE plugin. You may also want to check out our guide on how to create a site-specific WordPress plugin.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.