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 |1 Answer
Reset to default 0You 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
wp_enqueue_script()
to do so? – Pat J Commented May 19, 2020 at 15:06wp_enqueue_script
function to load the script. – Krishna Suwal Commented May 20, 2020 at 3:06