Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Add Custom Styles to WordPress Visual Editor

Ever wished you could create your own formatting buttons in WordPress, like a special note style or branded quote format? That’s exactly what custom styles do.

I’ve spent considerable time testing different approaches to custom styles in WordPress, from simple CSS additions to more advanced solutions.

This experience has helped me identify the most practical ways to improve your editor with custom formatting options that anyone on your team can use.

Today, I’ll walk you through how to add custom styles to your WordPress visual editor. You’ll learn what custom styles are, why they’re useful, and step-by-step instructions for adding them to your site, all based on my real-world testing experience.✨

Add Custom Styles to WordPress Visual Editor

Here’s a quick overview of what I’ll cover in this article:

Now let’s dive right in!

What Are Custom Styles and How They Can Change the Way You Use WordPress

Have you ever spent a ton of time adjusting fonts, colors, and buttons, only to realize you’ll have to redo it all next time?

WordPress offers some basic styling tools, but they don’t always match your brand or the exact look you’re going for. That’s where custom styles come in.

In simple terms, a custom style is a design shortcut within the WordPress editor. You set it up once, and then you can apply it anywhere on your site with just a click.

For example, you can create a custom font for headings, choose a background color for callout boxes, or apply a consistent style for testimonials.

WPForms' testimonials

Think of it as a template for design elements. Instead of manually adjusting padding, fonts, or colors every time, you pick your custom style, and it instantly applies your preferred design.

For example, let’s say you run a business blog and always include a product recommendation or disclaimer at the end of your posts. Instead of styling it each time, you could create a custom style called ‘Product Tip’ or ‘Important Note’ and apply it instantly.

This saves you time and ensures everything stays polished, especially if you’re working with a team or publishing a lot of content.

Having said that, let’s take a look at how to easily add custom styles to the WordPress visual editor.

How to Add Custom Styles to the WordPress Visual Editor (Block Editor)

If you want to add custom styles in the block editor, the most effective way to do it is using PHP code. But keep in mind that this approach is best suited for intermediate WordPress users.

That’s because you will need to edit your theme’s functions.php file, and even the smallest error can break your site. That said, you can use a beginner-friendly plugin to simplify the process.

I always recommend WPCode for this type of customization. In my opinion, it’s the best WordPress code snippets plugin on the market. I’ve thoroughly tested it and found it to be the easiest and safest way to add custom code without the risk of errors.

To learn more, see my team’s complete WPCode review.

🚨 Important Note: Regardless of your skill level, I always recommend creating a complete backup of your site before tinkering with any important code. See our guide on how to back up your WordPress website for instructions on how to do this.

Keep in mind that the code snippet I’ll be using is set up by default to add custom styles for the Button block. But with just a few minor tweaks, you can easily use it to support other blocks like:

🔷 Headings with fancy or outlined styles

💬 Quote, Pullquote, and Verse blocks

🗂️ Archives or Categories blocks

And more

This gives you a lot of flexibility to design reusable styles across your entire site.

First, you need to install and activate the WPCode plugin. If you need help, then see our beginner’s guide on how to install a WordPress plugin.

📓 Note: WPCode also has a free plan that you can use. However, upgrading to the pro version gives you access to features like a cloud library of code snippets, block snippet feature, smart conditional logic, and more.

Once you activate the plugin, head over to the Code Snippets » +Add Snippet page from the dashboard. Here, click the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.

Choose 'Add Your Custom Code (New Snippet)' option

This will direct you to the ‘Create Custom Snippet’ page, where you have to name your code snippet. This name will only be for your identification and won’t be shown to any users.

Then, choose PHP Snippet from the prompt that appears after clicking on the ‘Code Type’ option in the right corner.

Choosing PHP snippet in WPCode

After that, if you want to create a custom style for a button, add the following custom code into the ‘Code Preview’ box.

// Function to enqueue and register styles for both frontend and editor
function my_custom_button_styles() {
    // Register the style only once
    wp_register_style(
        'my-custom-button-styles',
        false
    );

    // Add inline styles
    wp_add_inline_style(
        'my-custom-button-styles',
        <<<CSS
        .is-style-fancy-button .wp-block-button__link {
            background-color: #f0c040;
            color: #fff;
            border: none;
            border-radius: 15px;
            padding: 10px 20px;
        }
        .is-style-outline-button .wp-block-button__link {
            background-color: transparent;
            color: #333;
            border: 2px solid #333;
            border-radius: 5px;
            padding: 10px 20px;
        }
        CSS
    );
    
    // Enqueue the style for both frontend and editor
    wp_enqueue_style('my-custom-button-styles');
}

// Function to add custom styles for Gutenberg editor
function my_custom_gutenberg_button_styles() {
    // Call the general styles function for the editor
    my_custom_button_styles();

    // Enqueue inline script for the editor to register block styles
    wp_add_inline_script(
        'wp-blocks',
        <<<JS
        wp.domReady(function() {
            wp.blocks.registerBlockStyle('core/button', {
                name: 'fancy-button',
                label: 'Fancy Button'
            });

            wp.blocks.registerBlockStyle('core/button', {
                name: 'outline-button',
                label: 'Outline Button'
            });
        });
        JS
    );
}

// Hook the styles function to frontend
add_action('wp_enqueue_scripts', 'my_custom_button_styles');

// Hook the custom block style registration
add_action('enqueue_block_editor_assets', 'my_custom_gutenberg_button_styles');

This code snippet adds two custom styles to the Buttons block in WordPress.

But here’s the cool part—you can easily customize it by swapping “button” with the name of any other block, and those custom styles will be applied to that block instead.

You can also tweak the code to change the background color, adjust the border radius, and make other style changes to match your needs.

Replace Button with any other block name

💡 Expert tip: If you are hesitant about customizing the code to fit other blocks instead of the Buttons block, I highly recommend using ChatGPT to help you. You can simply tell the AI tool to replace “button” with the block you prefer, and it’ll generate the custom code for you. Just copy and paste the code it provides!

It’s a proven tip that I’ve tried myself, and it works like a charm.

Now, go ahead and toggle the ‘Inactive’ switch to ‘Active’.

Finally, click the ‘Save Snippet’ button to store your changes.

Activate and save custom styles snippet

Next, open a post in the block editor and add the block for which you’ve added custom styles.

For example, if you’ve added custom styles for the Buttons block, go ahead and insert it into your post.

Once you do that, you’ll see the custom styles available in the ‘Styles’ section in the block panel.

Add custom styles in the block editor

After that, click the ‘Update’ or ‘Publish’ button to save your changes.

Then, visit your WordPress website to see the custom styles in action.

Custom Styles preview

Bonus: How to Add Custom Styles to WordPress Widgets

Now that you’ve learned how to add custom styles to the WordPress visual editor, let’s dive into another exciting feature: adding custom styles to WordPress widgets.

Customizing widgets allows you to make your site even more unique, whether it’s through colors, fonts, or layouts. This ensures that widgets match your site’s overall design, improving the user experience.

Block widget with custom style preview

The easiest way to add custom styles to WordPress widgets is by using WPCode.

The tool allows you to add CSS code to your site, including widget areas.

You can simply add custom CSS that targets specific widgets and adjust things like font sizes, background colors, padding, and even borders.

Custom CSS to style WordPress widgets

This is perfect for making sure your widgets match your overall design without having to get too technical. To get started, see our tutorial on how to add custom styles to WordPress widgets.

We hope this article helped you learn how to add custom styles in the WordPress visual editor. You may also want to see our beginner’s guide on how to create a custom WordPress block and our comparison on padding vs. margin in WordPress.

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

49 CommentsLeave a Reply

  1. Toufic Ahemmed

    This guys are awesome. they help me most of the time. thank you very very much.

    • WPBeginner Support

      Glad we could help :)

      Admin

  2. Aslan French

    This article should be updated with Gutenberg in mind, as this kind of functionality has been added to the Rich Text API recently.

    • WPBeginner Support

      Hi Aslan,

      Thanks for the suggestion. We will definitely look into older articles once Gutenberg is out.

      Admin

  3. Mehrdad

    Thanks a lot.
    This article really helped me.

  4. sabbi

    This adds classes to a p-tag doesnt it?
    Is there a way to even create own tags?
    Or another thing:will a chosen custom Format overwrite a previously chosen h-tag with a p? Would be necessary to prevent a client from getting in trouble when hes clicking around in the default AND custom styles.

  5. Britany

    Unfortunately this didn’t help me at all. Is there an article that explains what each of the fields you have to fill in mean?

    All I need is a way to apply the same formatting to quotes on my podcast show notes pages. Each quote is the the same font (that I have set-up as a headline style), bolded, and centered.

    I get tired of manually applying each part of the format to every quote and just a way to click one button and be done.

  6. Syams

    Please, tell me how to add a new font to WP visual editor.

  7. Moya Nicholson

    Thank you. Really helpful.

  8. stanley

    I need to modify my theme WP

    hell me please

  9. Sandy Connolly

    THANK YOU FOR THIS!!! OMG!!!! How helpful upon handing a site over to a client who wants to be able to add his/her own content based on our design!

  10. Marcin

    Hi, I develop WordPresses every day and do everything with WP but this piece of code is one of the most useful I’ve seen for long time… It is exactly what default WYSIWYG is (now I can say: was) missing! It allows to do some stunnig things inside content field. Thank You for sharing!

  11. Foxglove

    I have been using this plugin for a while now, but it is no longer available from wordpress.org — apparently no longer updated or supported. Is anyone aware of any alternatives other than the manual method you describe?

    • Lisa McMahon

      Here’s an updated/forked version of the original plugin mentioned in the article: TinyMCE Custom Styles

      • WPBeginner Support

        Hey Lisa, thanks for pointing this out. We have updated the article with the updated/forked version.

        Admin

  12. Shubha Das

    I want to add p instead of span. But when I change block name span to p, it doesn’t work.

    ‘title’ => ‘Note’,
    ‘block’ => ‘p’,
    ‘classes’ => ‘note’,
    ‘wrapper’ => true,

  13. MacKenzie

    I found this really helpful and I feel like I almost have it but it isn’t quite working yet. My text gets properly tagged with the span and class tags in the editor when selected – but none of the styles I put in the style sheet are being applied to the published note.

  14. Frank

    I always become nervous and feel like vomiting if some customer needs wordpress tweaks. Coll, this style pulldown BUT, as someone said before, completely useless if styles cannot be withdrawn afterwards and only add up, add up, add up…

    I’m used to these half baked sulotions in wordpress – normally there’s also a complementary half-baked solutions to half repair the first one. But here?

    Now HOW would you suggest TO REMOVE CUSTOM STYLES added via the styles pulldown. Code view is in no way acceptable for my client?

    I’d be unbelievably lucky if someone has found a way to do that and would share this knowledge (and for putting the catastrophe called wordpress out of my reach for( i hope) a very long time).

    Thanks a lot in advance!

    Frank

    • Chris

      Click on the style in the pulldown again. Done.

  15. Shafi ken

    Thanks for the helpful tutorial. Thanks

  16. K Shazzad

    Whole tutorial worked flawlessly for me. Thanks a bunch ;)

  17. Stef

    I tried out this plugin but realized, that when you change a custom style, It won’t update those you inserted already. You have to go back and reinsert them, because the feature adds the style inline and not via stylesheet

  18. Phil

    Thanks for the helpful tutorial. Is there any performance penalty in using the plugin rather than hand-coding it? Thanks,

    • WPBeginner Support

      No, but for reusing elements this method is quite handy. It is also useful, if you are building a site for a client and want them to be able to add things from the visual editor.

      Admin

  19. Sei

    Thanks for the tutorial!
    Is there a way to add two markups at once? Like, getting something like text

    • Sei

      Okay, your comments converts HTML. I mean, I’d like to get both ‘h1’ and ‘span’ markups around my text by clicking on only one style.

  20. Lily

    I’ve managed to do the custom classes and the elements do show with the right classes in the text editor and on the page, but the class isn’t applied in the visual editor which makes it very unclear whether it’s worked or not for the user. Is there any way to fix that?

  21. Bonnie Dasher-Andersen

    I’ve added two custom styles. When I go to edit a page, I have two Format menus, each one with the same entries (the two styles that I created). When I try to apply these styles, nothing happens. I can see the tag in the Text view, but when I view the page – the style hasn’t been applied.

    Any suggestions? Need to figure this out for a client who will be updated this WP site and is not very savvy.

    • Marcello

      I had the same problem, where the tags weren’t applied to code. Fixed it by setting ‘wrapper’ to ‘false’. I don’t know the technical reasons, just tested and it worked. Hope it helps!

      • hugotom

        I had the same problem that the style sheet is not recorded.
        Solution
        If you already have this recording style sheets in the functions.php file should add right there stylesheet custom-editor-style.css

        Example:

        function styles_theme(){
        wp_enqueue_style(‘bootstrap_css’, get_template_directory_uri() . ‘/sources/bootstrap/bootstrap.min.css’);
        wp_enqueue_style(‘main_css’, get_template_directory_uri() . ‘/style.css’);
        wp_enqueue_style(‘theme_css’, get_template_directory_uri() . ‘/custom/css/theme.css’);
        wp_enqueue_style(‘editor_css’, get_template_directory_uri() . ‘/custom-editor-style.css’); // HERE
        };

  22. Raphael Landau

    I’ve used this and also had the issue where the style/class is implemented to the entire Paragraph. This is because you set the style format as “block”. (‘block’ => ‘span’,).

    Quickly visiting the official Wordpress codex, discovers much more options for style formatting.

    http://codex.wordpress.org/TinyMCE_Custom_Styles

    Since span is an inline style be default, you should replace ‘block’ with ‘inline’, and viola! You’re styling should work as expected.

    so in short:

    array(
    ‘title’ => ‘Your Title’,
    ‘inline’ => ‘span’,
    ‘classes’ => ‘your-class’,
    ‘wrapper’ => true,
    ),

  23. nemaha

    Hi,
    great tutorial, thanks for that! I, too, have the problem, that style (a span) is applied to the whole paragraph. What I intend to do: Write a headline and format it as heading 1, then mark only one specific word within that headline to add a custom style. Any update on how to fix this? Thanks!

  24. bekee

    i, too, have the problem where it applies the style to the whole paragraph, not just the selected element. any update on this? thanks!

  25. Debbie

    Having the same issue. Highlight one word, but takes effect on entire paragraph.

  26. James

    I find a couple of problems. It does seem to work, but not as expected. Will not do for someone who does not know code.

    1. Highlight a single word in a paragraph to add a but the is added to the entire paragraph, not just the highlighted word.
    2. No way to remove the css without editing code. My client does not do code! Even tried to make a class of .nothing but the new class is only added to any others, does not replace existing class.

  27. Sheikh Zuhaib Siddiqui

    Hey I have an error in add media. When I try to upload any media, there is only continue loading and not showing up any media and can upload media……………Let me know what the mistake here???

    But this is working fine …………just error in media uploader. please provide me this solution.

  28. Marlice

    Hi. Thank you for this great tutorial. I have a problem with content that is already in the editor. If I mark a word or a part of text and choose a style (for example “blue button” – from your code) it wraps not only the marked word or part of text. Instead it marks the whole content and put a span with the class .blue button on it. I tried it several times with other pages and posts – always the same: if the content was already there and I marked it, then the whole content get the span class. This does not happen if I wrap a new edited text in the page/post – than everything works fine. Does anybody has this phenomen too?
    Thank you,
    Ute

  29. John-Henry Ross

    Hi. I tried this method and it works like a charm, exactly what I was looking for, thank you. I just want to find out if there is a way to add styles to a subfolder instead of just adding it under a format button. E.g. add a headings subfolder with all heading stylings, add div submenu with div stylings, etc.

  30. dave

    Thanks!
    It’s great to show 2 methods, too… my clients cower in fear at handling any code.

    I will sometimes use custom fields to “force” safe additional styles, but the TinyMCE can be handled by some people, so I’ll kee that in mind. :)

    Ciao, Dave

  31. WPBeginner Staff

    you do not need to add the dot in

    classes’ => ‘.alert-blue’,

    It should be

    classes’ => ‘alert-blue’,

      • WPBeginner Staff

        Can you paste the code you added in your functions.php file. Also paste the CSS you are using for these buttons.

        • Jeff Gaudette

          Sure. Functions:

          /*
          * Callback function to filter the MCE settings
          */

          function my_mce_before_init_insert_formats( $init_array ) {

          // Define the style_formats array

          $style_formats = array(
          // Each array child is a format with it’s own settings
          array(
          ‘title’ => ‘Alert’,
          ‘block’ => ‘span’,
          ‘classes’ => ‘entry p.alert-blue’,
          ‘wrapper’ => true,

          ),
          array(
          ‘title’ => ‘Alert Blue’,
          ‘block’ => ‘p’,
          ‘classes’ => ‘alert-blue’,
          ‘wrapper’ => true,
          ),
          array(
          ‘title’ => ‘Blue Button’,
          ‘block’ => ‘span’,
          ‘classes’ => ‘alert-blue-button’,
          ‘wrapper’ => true,
          ),
          );
          // Insert the array, JSON ENCODED, into ‘style_formats’
          $init_array[‘style_formats’] = json_encode( $style_formats );

          return $init_array;

          }
          // Attach callback to ‘tiny_mce_before_init’
          add_filter( ‘tiny_mce_before_init’, ‘my_mce_before_init_insert_formats’ );

          CSS

          .alert-blue{
          background: none repeat scroll 0 0 #E7F4FD;
          border: 1px solid #C5D7E3;
          color: #3A5971;
          font-size: 18px;
          line-height: 24px;
          text-align: center;
          margin: 0 0 15px !important;
          padding: 15px 25px;
          width: auto;
          }

  32. WPBeginner Staff

    Did you add the CSS in your stylesheet?

    Make sure that the style rules you add in your CSS match the classes you add in the Callback function to filter the MCE settings.

  33. Kemi O

    TinyMCE Advanced Professsional Formats and Styles is only supported up to WP version 3.6.1

  34. Keely Worth

    I avoid the Visual Editor like the plague! Only use it if I have to switch to it to use a theme’s built-in shortcode button. Otherwise – never use it.

  35. Blair2004

    I have tried first method, new style are available but, while i’m selecting one, nothing happens…

  36. Jeff Gaudette

    I am trying to do this with a block and having no luck. Code is:

    array(
    ‘title’ => ‘Alert Blue’,
    ‘block’ => ‘p’,
    ‘classes’ => ‘.alert-blue’,
    ‘wrapper’ => true,
    ),

    It works when I use span, but this necessitates a span class, which I can’t use.

    When i use the above code, nothing happens in the wp editor. I select the text, click the Alert Blue formatting option and nothing happens: http://screencast.com/t/dijujZ2ZdqBy

    Any advice?

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.