There are several questions about this (like this, or that), but none of them provides a solution to me.
The situation:
I have a metabox. Inside this metabox I want to dynamically add tinyMCE editors.
The code:
PHP:
add_action('wp_ajax_insert_tinymce', 'insert_tinymce');
function insert_tinymce(){
wp_editor('','editor-id');
exit;
}
JS:
$.post(
global.ajaxurl,
{
action : 'insert_tinymce'
},
function(response) {
$(response).appendTo('#target-div');
console.log(tinyMCE); // no instance of 'editor-id'
console.log(tinyMCEPreInit.mceInit); // 'editor-id' not listed
console.log(tinyMCE.activeEditor); // null
}
);
The result is a editor window (with html and visual tabs), a upload button and a textarea but completly without buttons.
What I tried (as suggested in the other questions):
// In the response function
tinyMCE.init({
skin : "wp_theme",
mode : "exact",
elements : "editor-id",
theme: "advanced"
});
// In the response function
tinyMCE.init(tinyMCEPreInit.mceInit['editor-id']);
// in the PHP
ob_start();
wp_editor( '', 'editor-id' );
echo ob_get_clean();
This first one works, but I would have to set many settings manually, plus my custom buttons don’t show up.
The second one obviously doesn’t work, because there is no editor-id
listed in tinyMCEPreInit.mceInit
The third one (output buffering) makes no difference.
I have noticed that wp_editor()
adds the tinyMCE instances to tinyMCEPreInit
and adds it via admin_print_footer_scripts
hook. So I would have to dynamically update tinyMCEPreInit
(without losing the instance of the main editor) and then fire tinyMCE.init(tinyMCEPreInit.mceInit['editor-id'])
– MAYBE
Any help highly appreciated 😉
Thanks!
your post here helped me figure a solution. Along with the mceInit property, I also had to retrieve the qtInit that wp_editor generates as well.
The following is the relevant code within my class. Basically I have wp_editor run so that the javascript is generated. I use hooks to retrieve the javascript, so that I can echo it out in the ajax response.