Loading text in tinyMCE for Wordpress via AJAX
I am using tinyMCE for Wordpress.
What's the way to download text from server via AJAX?
So far I have:
PHP:
<?php echo the_editor($_POST ? $_POST['content'] : '', $id = 'content'); ?>
javascript (which doesn't work ...):
$("select[name='tpl']").live("change", function(e) {
var file = $(this).val();
var loadUrl = varsJs.WORDPRESS_PLUGIN_URL + "/templates/" + file;
$.get(loadUrl, function(result) {
$("#content").val(result);
});
});
The variable is result
loaded with the desired text. No problem with that. But how do you transfer this content to minyMCE?
source to share
if (typeof tinymce === "object"){
$("select[name='tpl']").live("change", function(e) {
var file = $(this).val();
var loadUrl = varsJs.WORDPRESS_PLUGIN_URL + "/templates/" + file;
$.get(loadUrl, function(result) {
tinymce.get("content").focus();
tinymce.activeEditor.setContent(result);
});
});
}
Note. varsJs
- the second parameter of the function wp_localize_script
, used to transfer data from php
to javascript
. Not really needed in this exact problem, but useful to know it.
source to share
Try this code where " content " is your #ID field
tinymce.init (tinyMCEPreInit.mceInit ['content']);
this way, and once tinymce is also loaded into the current html, you will only restore one field that you received from the Ajax request.
also set this code before saving the ajax Call
tinymce.activeEditor.save (); // get the editor instance
source to share