4

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?

2 Answers 2

3
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.

Sign up to request clarification or add additional context in comments.

1 Comment

For a general approach only 2 lines did the trick for me. Your data object returned from your ajax call will have the text you want in your editor. Use these lines tinymce.get("#idOfYourTextArea").focus(); and tinymce.activeEditor.setContent(data.content); where data.content is your dat you want to be stored in the tinyceMCE editor
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.