Skip to content

Instantly share code, notes, and snippets.

@siamkreative
Created November 28, 2014 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siamkreative/e29ea4f4d761a4185494 to your computer and use it in GitHub Desktop.
Save siamkreative/e29ea4f4d761a4185494 to your computer and use it in GitHub Desktop.
A simple script to check whether TinyMCE is active and visible in WordPress Dashboard
jQuery(document).ready(function ($) {
'use strict';
/*
References
http://codeblow.com/questions/method-to-check-whether-tinymce-is-active-in-wordpress/
https://wordpress.org/support/topic/tinymceactiveeditorgetcontentcontent-does-not-work-with-tinymce-advanced
*/
var tinymceActive, cannedResponse, editorType;
/* This is an object passed using wp_localize_script that returns either "tinymce" or "html" (from wp_get_user_setting) */
editorType = wpascr.editor;
$('.wpas-canned-response').click(function (e) {
e.preventDefault();
tinymceActive = (typeof tinyMCE != 'undefined') && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden();
cannedResponse = $(this).attr('data-message');
/* Check if TinyMCE is active */
if (tinymceActive) {
/* Check version of TinyMCE */
if (tinymce.majorVersion < 4) {
tinyMCE.execInstanceCommand('wpas_reply', 'mceInsertContent', false, cannedResponse);
} else {
var edv4 = tinyMCE.get('wpas_reply');
edv4.insertContent(cannedResponse);
}
} else {
/* Append canned response to existing textarea value */
$('#wpas_reply').val(function (i, val) {
return val + cannedResponse;
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment