Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to check whether TinyMCE is active in WordPress

I'm working on a plugin that, when TinyMCE is in use as the Visual editor, uses TinyMCE commands to insert text into body content editing area. Currently, it works by just running the command. If it works, then TinyMCE is active and if not, then I have custom JS for working with the HTML editor.

My question, however: is there any way to check whether TinyMCE is active or not instead of just running the command and having it fail when it isn't?

like image 454
Daniel Bachhuber Avatar asked Jul 24 '09 17:07

Daniel Bachhuber


People also ask

How do I know what version of TinyMCE I have?

If you check on the source code of TinyMCE in the official repository at Github, specifically on this file, you will find 2 properties on the definition of the TinyMCE object: majorVersion : contains the Major version of TinyMCE build. minorVersion : contains the Minor version of TinyMCE build.

How do I get TinyMCE content?

The TinyMCE getContent and setContent methods You can do this using the getContent() API method. Let's say you have initialized the editor on a textarea with id=”myTextarea”. This will return the content in the editor marked up as HTML.


1 Answers

And... I've answered the question for myself. The conditional you want to test for is as follows:

is_tinyMCE_active = false;
if (typeof(tinyMCE) != "undefined") {
  if (tinyMCE.activeEditor == null || tinyMCE.activeEditor.isHidden() != false) {
    is_tinyMCE_active = true;
  }
}

The trick is that tinyMCE.activeEditor returns null when TinyMCE isn't activated. You can use the isHidden() method to make sure it isn't executing when you've switched back to HTML editor mode.

This is poorly documented on the TinyMCE website and forums.

like image 147
4 revs, 3 users 77% Avatar answered Oct 21 '22 20:10

4 revs, 3 users 77%