Loading text in tinyMCE for WordPress via AJAX

I am using tinyMCE for WordPress.
Which is the way to load text from server via AJAX?
Until now I have:

php:

Read More
<?php echo the_editor($_POST ? $_POST['content'] : '', $id = 'content'); ?>

javascript (which is failing…):

$("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 result is loaded with the desired text. No problem with that. But how pass this content to the tinyMCE?

Related posts

Leave a Reply

2 comments

  1. 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 is the second parameter of wp_localize_script function used to pass data from php to javascript. Really no needed in this precise issue but useful to know it.

  2. Try this code, where ‘content‘ is your field #ID

    tinymce.init(tinyMCEPreInit.mceInit[‘content’]);

    this way and once tinymce is also loaded in current html,
    you will reinit only one field, the one you received from Ajax Request.

    also set this code before ajax saving Call

    tinymce.activeEditor.save(); // get editor instance