https://stackoverflow.com/questions/22813970/typeerror-window-tinymce-execinstancecommand-is-not-a-function
幸亏
Scott B在Wordpress 3.9中,TinyMCE更新为版本4,在TinyMCE 4中,方法“execInstanceCommand”已被方法“execCommand”替换。
对于兼容性问题(与旧版本的WP),必须检查TinyMCE版本并使用合适的方法。
在下面,我更改了您的代码。注释的代码行是我的。
function submitData($form) {
try {
$form = $form || jQuery(\'form\');
if(window.tinyMCE) {
/* get the TinyMCE version to account for API diffs */
var tmce_ver=window.tinyMCE.majorVersion;
var selectedContent = tinyMCE.activeEditor.selection.getContent(),
id = tinyMCE.activeEditor.editorId || \'content\',
shortcodeName = $form.attr(\'name\'),
shortcode = \' [\' + shortcodeName + \' \';
$form.find(\'[data-name]\').each(function() {
var $this = jQuery(this),
type = $this.data(\'type\'),
value = ($this.attr(\'type\') == \'checkbox\')
? ($this.is(\':checked\')) ? \'on\' : \'\'
: $this.val() || \'\';
value = fitValue(type, value);
shortcode += $this.data(\'name\') + \'="\' + value + \'" \';
});
shortcode += \']\' + selectedContent + \'[/\' + shortcodeName + \'] \';
/* Check for TinyMCE version */
if (tmce_ver >= 4) {
/* In TinyMCE 4, we must be use the execCommand */
window.tinyMCE.execCommand(\'mceInsertContent\', false, shortcode);
} else {
/* In TinyMCE 3x and lower, we must be use the execInstanceCommand */
window.tinyMCE.execInstanceCommand(id, \'mceInsertContent\', false, shortcode);
}
tinyMCEPopup.editor.execCommand(\'mceRepaint\');
tinyMCEPopup.close();
}
} catch (e) {
console.error(e);
}
return;
}