Igor Parra
Igor Parra

Reputation: 10348

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:

<?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?

Upvotes: 4

Views: 1635

Answers (2)

Jo&#227;o Cunha
Jo&#227;o Cunha

Reputation: 11

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

Upvotes: 0

Igor Parra
Igor Parra

Reputation: 10348

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.

Upvotes: 3

Related Questions