Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm developing a plugin that I'll install for all my clients to make WordPress slightly easier to use for them. One of the things it will change is the default TinyMCE configuration. It'll add things like underline and justifyfull to the first TinyMCE row.

I know I could use other plugins like TinyMCE Advanced to alter TinyMCE for my clients, but they add a lot of unneccessary bloat which is susceptible to problems during updates. I want to make this as minimalistic as possible in order to avoid update issues.

I've written this simple code that mostly works great:

function mo_change_mce_buttons( $initArray ) {
    $initArray['theme_advanced_buttons1'] = 'bold,italic,underline,strikethrough,|,bullist,numlist,blockquote,|,justifyleft, justifycenter,justifyright,justifyfull,|,link,unlink,wp_more,|,spellchecker,fullscreen,wp_adv';
    return $initArray;
}
add_filter('tiny_mce_before_init', 'mo_change_mce_buttons');

This code works great except for one huge problem. It disables any TinyMCE plugins on the TinyMCE row in question (theme_advanced_buttons1). The Vipers Video Quicktags plugin for example, which adds a YouTube-button, is removed by using the code above.

How can I modify the default first TinyMCE row using simple code in my own plugin without sacrificing the possibility for other plugins to add buttons to it too?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

tiny_mce_before_init is a filter that gets the whole TinyMCE configuration, but there are filters that act on smaller parts of it, like mce_buttons for only the buttons. The advantage here is that they act on arrays, so it's easy to add or remove parts from them. This is how Vipers Video Quicktags does it too.

You can change your code to the following snippet to insert the buttons at the right places and not remove buttons that other plugins have added:

add_filter( 'mce_buttons', 'wpse17686_mce_buttons' );
function wpse17686_mce_buttons( $old_buttons )
{
    $new_buttons = array();
    foreach ( $old_buttons as $button ) {
        $new_buttons[] = $button;
        if ( 'italic' == $button ) {
            $new_buttons[] = 'underline';
        }
        if ( 'justifyright' == $button ) {
            $new_buttons[] = 'justifyfull';
        }
    }
    return $new_buttons;
}
share|improve this answer
    
Thanks! That solved it! I was mistakenly under the impression that mce_buttons only could add new buttons, I didn't know it could be used to change the default config. –  kristofferR May 19 '11 at 15:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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