1

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.

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){}
            }
        }
    })();
3
  • just skipped through your code, but is tinyMCEPreInit.mceInit[ed] actually set? Feb 6, 2012 at 22:05
  • The snippet I pasted above is generated from the core WP file edit-tags.php?action=edit&taxonomy=category
    – N2Mystic
    Feb 6, 2012 at 22:33
  • I've found that the "mce" variable is set to false whenever I've left the main post/page editor on the "HTML" tab view.
    – N2Mystic
    Feb 6, 2012 at 22:34

1 Answer 1

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.

1
  • +1 for a brilliant solution. This has the effect of forcing the rich text editor to appear regardless if the last setting was HTML or Visual.
    – N2Mystic
    Jun 1, 2012 at 20:57

Your Answer

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

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