WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to insert a shortcode in the Wordpress Post Editor from javascript (not a tinymce plugin). I currently use:

tinyMCE.activeEditor.execCommand('mceInsertContent', false, "[SHORTCODE]test[/SHORTCODE] ");

but this only seems to work when the editor is in "Visual" mode (and sometimes activeEditor == null). How do I accomplish this regardless of the state of the editor? (I'd think this is a fairly common scenario)

share|improve this question

I was pulling my hair out on this one, but I might have figured it out.

Try this (with jQuery):

if( ! tinyMCE.activeEditor || tinyMCE.activeEditor.isHidden()) {
  jQuery('textarea#content').val("[SHORTCODE]test[/SHORTCODE] ");
} else {
  tinyMCE.execCommand('mceInsertRawHTML', false, "[SHORTCODE]test[/SHORTCODE] ");

Basically, you need to set the value of the textarea directly if the editor is hidden.

share|improve this answer

Your Answer


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.