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?

+3


source to share


2 answers


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.

+2


source


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

0


source







All Articles