Rich text editor settings persist throughout all rich text editors

I have an issue I’m trying to better understand in order to force mce to true in the script check below.

Background: I’m applying a rich text editor to the category description textarea to allow users to apply rich text formatting to the category descriptions.

Read More

The problem I’m having is when the user happens to leave the post or page editor on the “HTML” tab. In this case, WordPress apparently is setting a global flag that results in the mce variable in the script below to initialize as false.

This has the effect of disabling my rich text editor on the category description field.

How can I overcome this?

<script type="text/javascript">
    (function(){
        var init, ed, qt, first_init, mce = false;

//mce needs to be true!!!

        if ( typeof(tinymce) == 'object' ) {
            // mark wp_theme/ui.css as loaded
            tinymce.DOM.files[tinymce.baseURI.getURI() + '/themes/advanced/skins/wp_theme/ui.css'] = true;

            for ( ed in tinyMCEPreInit.mceInit ) {
                if ( first_init ) {
                    init = tinyMCEPreInit.mceInit[ed] = tinymce.extend( {}, first_init, tinyMCEPreInit.mceInit[ed] );
                } else {
                    init = first_init = tinyMCEPreInit.mceInit[ed];
                }

                if ( mce )
                    try { tinymce.init(init); } catch(e){}
            }
        }

        if ( typeof(QTags) == 'function' ) {
            for ( qt in tinyMCEPreInit.qtInit ) {
                try { quicktags( tinyMCEPreInit.qtInit[qt] ); } catch(e){}
            }
        }
    })();

Related posts

Leave a Reply

1 comment

  1. That ‘global flag’ is called a cookie 🙂

    I struggled with the same issue a couple of weeks back, looking for AJAX calls, site options and user preferences until I figured it out.

    If you check WP’s source code for generating the first line of the JS function you’re having trouble with, you’ll see that ‘true’ or ‘false’ are a result of the wp_default_editor function, which WP allows you to filter. Adding this to your functions.php will cause WP to ignore the fact that someone last used the HTML tab when editing posts, and will make TinyMCE the default editor every time (assuming the page is rendered in a device which can take advantage of Rich Text editing, which WP checks for):

    add_filter('wp_default_editor', 'always_start_with_tinymce');
    function always_start_with_tinymce($editor_type) {
        if(user_can_richedit()) {
            return 'tinymce';   
        }
        return $editor_type;
    }
    

    It worked well for me because I had to force Rich Text editing in a textarea which had no HTML editing option, but it was an entirely separate page from the other TinyMCE instances… maybe your case will require some more checks inside the function above, but hopefully it’ll take you down the right path.