1

I've created a TinyMCE style dropdown for the TinyMCE WordPress editor.

The formats are appearing as expected on the editor but when the drop down is expanded to choose from a style, they are showing as disabled - would there by any reason for this? My code is attached below.

Drop down options showing grey & disabled

function new_mce_button($buttons) {

    array_unshift($buttons, 'styleselect');
    return $buttons;

}
add_filter('mce_buttons_2', 'new_mce_button');

/**
 * Callback function to filter the MCE settings
 */
function custom_formats( $init_array ) {  

    $style_formats = array(
        array(
            'title' => 'White Button',
            'selector' => 'a',
            'classes' => 'cta wht',
        ),
        array(
            'title' => 'Grey Button',
            'selector' => 'a',
            'classes' => 'cta gry'
        ),
        array(
            'title' => 'Light Blue Button',
            'selector' => 'a',
            'classes' => 'cta lblue'
        ),
        array(
            'title' => 'Dark Blue Button',
            'selector' => 'a',
            'classes' => 'cta dblue'
        ),
    );  

    $init_array['style_formats'] = json_encode( $style_formats );  

    return $init_array;  

} 
add_filter( 'tiny_mce_before_init', 'custom_formats' );
2
  • In your function you've set the selector to work only in anchor (a) tags.
    – Karun
    Commented Aug 4, 2015 at 12:40
  • Thanks, @Karun - worked a treat when I updated from 'selector' => 'a' to 'block' => 'div'. Can't believe I couldn't see it!
    – estin92
    Commented Aug 4, 2015 at 13:42

1 Answer 1

1

As pointed out by @karun, the text first needed to be made into a hyperlink, after which the formats became active.

Changing the array values to

array(
  'title' => 'Grey Button',
  'block' => 'div',
  'classes' => 'cta gry'
),

made the style permanently available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.