Custom styles for WordPress TinyMCE

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 instructions from the codex 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… )

Read More

I managed to get the code working, but only using the commented-out line in my_mce_before_init(). The problem with this version is that it adds the class with a generic <span>. 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(
        array(
            'title' => 'Section Head',
            'block' => 'h3',
            'classes' => 'section-head'
        ),
        array(
            'title' => 'Sub Section Head',
            'block' => 'h4',
            'classes' => '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');

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

Related posts

Leave a Reply

3 comments

  1. It seems you’re using this (awesome) tutorial: http://alisothegeek.com/2011/05/tinymce-styles-dropdown-wordpress-visual-editor/
    Worked great for me so I compared your code with mine: it seems you lack a

    'wrapper' => true
    

    as a fourth parameter to each sub-array. This is needed for adding a class on a parent element of your selection (it can broaden your selection) and not creating a new element around your exact selection before adding it a class.
    Thus if you select part of a paragraph or part of 2 paragraphs, it’ll select the whole paragraph(s) (not so sure about the 2 paragraphs thing, please test 🙂 but at least it won’t create inline element around your exact selection).

    From the documentation (above link):

    wrapper [optional, default = false]
      if set to true, creates a new block-level element
      around any selected block-level elements
    

    My customization:

    $style_formats = array(
        array(
            'title' => 'Column',
            'block' => 'div',
            'classes' => 'col',
            'wrapper' => true
        ),
        array(
            'title' => 'Some div with a class',
            'block' => 'div',
            'classes' => 'some_class',
            'wrapper' => true
        ),
        array(
            'title' => 'Title with other CSS',
            'selector' => 'h3',
            'classes' => 'other_style'
        ),
        array(
            'title' => 'Read more link',
            'selector' => 'a',
            'classes' => 'more'
        )
    );
    

    Hope it works for you

  2. AHA!

    I found the problem: the two different versions of defining the custom classes must be added to different keys in the settings array.

    This version…

    "Section Head=section-head;Sub Section Head=sub-section-head";
    

    …needs to be the value of 'theme_advanced_styles'.

    Whereas this version…

    $style_formats = array(
    array(
        'title' => 'Column',
        'block' => 'div',
        'classes' => 'col',
        'wrapper' => true
        ),
    );
    

    …needs to be the value of 'style_formats' in the TinyMCE settings array.

    I had changed to the second style but hadn’t noticed the different key for the array.