Custom styles for Wordpress TinyMCE


Staff member
I've read several tutorials for adding custom styles to the WYSIWYG (TinyMCE) editor. None of them seem to work in the newest version(s) of Wordpress. I'm using v3.3.2. The <a href="" rel="nofollow">instructions from the codex</a> work, but in a limited way...

NOTE: To be 100% clear, I'm trying to add a "Styles" dropdown which the author can use to apply my custom styles to the text. (Please don't confuse my question with how to style the editor its self, using editor-style.css... )

I managed to get the code working, but only using the commented-out line in
. The problem with this version is that it adds the class with a generic
. I'm trying to use the more powerful version of the code (as shown below), but something isn't quite right. The styles drop-down box appears, but it's blank. If I click it the first item is says "Styles" but does nothing. I suspect there's something off about my array. Hopefully someone more knowledgeable than me can set me straight.

Here's the relevant code in my theme's functions.php...

Here's how I add the button:

// Add the Style selectbox to the second row of MCE buttons
function my_mce_buttons_2($buttons)
    array_unshift($buttons, 'styleselect');
    return $buttons;
add_filter('mce_buttons_2', 'my_mce_buttons_2');

Here's how I add the styles (it works when I uncomment the ):

//Define the actual styles that will be in the box
function my_mce_before_init($init_array)
    // add classes using a ; separated values
    //$init_array['theme_advanced_styles'] = "Section Head=section-head;Sub Section Head=sub-section-head";

    $temp_array['theme_advanced_styles'] = array(
            'title' =&gt; 'Section Head',
            'block' =&gt; 'h3',
            'classes' =&gt; 'section-head'
            'title' =&gt; 'Sub Section Head',
            'block' =&gt; 'h4',
            'classes' =&gt; 'sub-section-head'

    $styles_array = json_encode( $temp_array['theme_advanced_styles'] );

            //  THIS IS THE PROBLEM !!!! READ BELOW
    $init_array['theme_advanced_styles'] = $styles_array;

    return $init_array;
add_filter('tiny_mce_before_init', 'my_mce_before_init');

<strong>UPDATE: I figured it out (see my answer below). Before you scroll down, notice in the code above,
is the wrong key. It should be
when defining the custom styles in the way that I'm doing. I suspect this is a common mistake.</strong>