WordPress.org

Ready to get started?Download WordPress

Codex

Plugin API/Filter Reference/tiny mce before init

Description

This filter grants developers access to the TinyMCE settings array.

Example

The following example adds custom style options to an existing Style dropdown.

Note that, by default, the Style dropdown is hidden in WordPress. To show this option, you will need to add it using the mce_buttons_2 filter AND load the CSS using the mce_css hook.

function mytheme_tinymce_settings($settings)
{
    //First, we define the styles we want to add in format 'Style Name' => 'css classes'
    $classes = array(
        __('Test style 1','mytheme')     => 'teststyle',
        __('Test style 2','mytheme')     => 'teststyle',
        __('Test style 3','mytheme')     => 'teststyle',
    );

    //Delimit styles by semicolon in format 'Title=classes;' so TinyMCE can use it
    if ( ! empty($settings['theme_advanced_styles']) )
    {
        $settings['theme_advanced_styles'] .= ';';
    }
    else
    {
        //If there's nothing defined yet, define it
        $settings['theme_advanced_styles'] = '';
    }

    //Loop through our newly defined classes and add them to TinyMCE
    $class_settings = '';
    foreach ( $classes as $name => $value )
    {
        $class_settings .= "{$name}={$value};";
    }

    //Add our new class settings to the TinyMCE $settings array
    $settings['theme_advanced_styles'] .= trim($class_settings, '; ');

    return $settings;
}
add_filter('tiny_mce_before_init','mytheme_tinymce_settings');

Related