javascript - How to set callback to execute after all the tinyMCE editors have been initialized?

I've loaded a tinyMCE editor using wp_editor function of Wordpress.Now, I want to set value of that editor after it

I've loaded a tinyMCE editor using wp_editor function of Wordpress.

Now, I want to set value of that editor after it gets initialized. I tried to do it like this:

$(function() {
    tinymce.get(...).setContent(...);
});

But it throws an error saying Cannot read property 'setContent' of undefined because the editor has not been initialized. To confirm it I console logged using console.log( tinymce.editors.length ) statement and it prints 0 but later when I inspected the variable tinymce.editors using browser console after the loading was done, the editor was there and I could manipulate it.

So, my conclusion was to wait for all the tinyMCE editors to be initialized then run the above code to change the editor's value. Note that I need to set the value using JS, not from the backend (php).

EDIT: I'm loading the JS scripts using following statement:

add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

And inside the enqueue_scripts function:

wp_enqueue_script(..., ..., true);

Please tell me if there's a way to accomplish this. And ask me if you need more information or I'm unclear.

Thanks in advance :)

I've loaded a tinyMCE editor using wp_editor function of Wordpress.

Now, I want to set value of that editor after it gets initialized. I tried to do it like this:

$(function() {
    tinymce.get(...).setContent(...);
});

But it throws an error saying Cannot read property 'setContent' of undefined because the editor has not been initialized. To confirm it I console logged using console.log( tinymce.editors.length ) statement and it prints 0 but later when I inspected the variable tinymce.editors using browser console after the loading was done, the editor was there and I could manipulate it.

So, my conclusion was to wait for all the tinyMCE editors to be initialized then run the above code to change the editor's value. Note that I need to set the value using JS, not from the backend (php).

EDIT: I'm loading the JS scripts using following statement:

add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

And inside the enqueue_scripts function:

wp_enqueue_script(..., ..., true);

Please tell me if there's a way to accomplish this. And ask me if you need more information or I'm unclear.

Thanks in advance :)

Share Improve this question edited May 20, 2020 at 3:03 Krishna Suwal asked May 19, 2020 at 14:44 Krishna SuwalKrishna Suwal 154 bronze badges 2
  • Can you please edit your question to show us how you're loading the JavaScript? Specifically, are you using wp_enqueue_script() to do so? – Pat J Commented May 19, 2020 at 15:06
  • @PatJ yes i'm using the wp_enqueue_script function to load the script. – Krishna Suwal Commented May 20, 2020 at 3:06
Add a comment  | 

1 Answer 1

Reset to default 0

You can wait for the specific editor manager to get added and then bind the init event handler to set content of the editor like this:

tinymce.on( 'addeditor', e => {
    if ( e.editor.id === <your_editor_id> ) {
        e.editor.on( 'init', event => {
            event.target.setContent( <your_editor_content> );
        });
    }
}, true );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742418621a4440231.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信