Custom TinyMCE Editor Formatting in WordPress

Sometimes when writing a block of text using the WordPress editor, you need it to look a certain way, we usually do this by adding styles to peices of text, but we don’t always want our clients to have to open up the text editor and do this manually.

In this example we will create a small filter that you can add to your functions.php file to give the user some extra formatting options, to add a custom ‘Lead’ paragraph style.

Using the Custom TinyMCE Formatting

Here is a super quick example of the filter in action. It shows you:

  • How to toggle the toolbar so that you can see the Formats menu
  • How to apply the formatting using the Formats menu
  • How to clear the styles

The Code

The code is really simple, just hook into tiny_mce_before_init and add your custom style to the style_formats array. Be sure to include a title, the type of element you want to use, and most importantly the class.

Styles

Of course, nothing will happen without a little styling. Be sure to add the styles to both your main WordPress styles.css so your Lead shows on the Front End, and also be sure to register an editor style sheet so that the code shows in the editor (You can use add_editor_style() to do this).

Posted by Matt Watson

Matt Watson is co-founder of the WordPress agency Make Do. Matt loves learning about personal, professional and web development. Learn more about Matt.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.