1

I'm trying to add additional wp_editor instances when a user clicks an "Add content" button in the back-end. I have it mostly working, except that the user needs to save the post after adding the field in order for the TinyMCE editor to appear, as it's initialized with the PHP wp_editor() function.

After some digging, I found the wp.editor.initialize() (docs) JavaScript function, which seems like it should initialize a new editor given a textarea's ID.

This doesn't seem to work, no matter what I try. I have the following code:

<textarea id="custom-tinymce-testing" name="custom-tinymce-testing"></textarea>

Which I try to initialize with JavaScript after clicking a button:

wp.editor.initialize("custom-tinymce-testing", {
    mediaButtons: true,
    tinymce: true,
    quicktags: true,
});

This doesn't appear to do anything; the textarea remains unchanged, and no console errors are thrown. Running console.log(wp.editor.initialize()) does show that the function exists, so I don't think it's an ordering thing.

What am I doing wrong here?

1 Answer 1

1

This was way simpler than I thought it was – even on post.php, you need to run wp_enqueue_editor() before using wp.editor in JavaScript. I had assumed that I wouldn't need to do that, because an editor was already loaded.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.