Web Development WordPress

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 pieces 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
TinyMCE Formatting in action
TinyMCE Formatting in action

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).

Matt Watson loves to talk about Health, Wealth and Code (mainly WordPress). Get in touch with Matt or follow Matt on Twitter to get notified about his latest posts.

2 Comments

  1. Hi, how to add the same custom formats in gutenberg/classic editor block?

    • Matt Watson Reply

      Hi Stratboy,

      It can be done in Gutenberg by adding extensions. Something I will cover in a future article.

Write A Comment