I have a theme that is custom developed and really complex. One of the things that I have is multiple content areas where users can specify content for specific tabs. I load multiple instances of the WordPress editor through the wp_editor()
function. It works perfectly. (This is all on the admin side, in the “Page” post type)
However, I began making a few improvements, including the ability to add/remove tabs dynamically (before, I loaded 6 editors on the page). Users may have 1-7 tabs.
When users add a tab, it needs to add an instance of the editor to the page. However, no matter what I try, I cannot get it to load and display correctly.
Here are the 2 things that I have tried so far:
- Create a php file that has the admin bootstrap included, and then loading the editor with
wp_editor()
. I then do a jQuery$.load
to call the page and include the resulting HTML in the area that it needs to display. This doesn’t really work, however, as the editors formatting buttons disappear (it’s worth noting, that pulling the page up directly, the editor displays and functions perfectly) - Loaded the editor on the page, inside a hidden div, and then once a tab is added, use jquery to move it into place. This loads the editor in tact, but you cannot use any of the editor buttons (they display, but don’t do anything), and you can’t put your cursor in the text area (curious, however, that switching to HTML mode allows typing and some interaction with the HTML mode buttons)
So the question is, has anyone had any luck adding editors through AJAX calls? Any advice?
To get the quicktags to show up, you need to re instantiate them within your ajax oncomplete handler.
My ajax success handler looks like this;
I’ve managed to get the editor to load by first calling a static function that creates the editor and caches it as variable. I run the editor creation method on init. This seems to get wordpress to enque all the required scripts.
Its important that when you create your editor instance, that you set it to use tinymce, that way the tinymce js file is enqued as well.
After struggling with it, found the solution which works, in a callback after you add new element:
Its strange that there is zero documentation inside codex.
Finally, working solution:
add action in wordpress, lets say
My_Action_Name
(also note, textarea IDMy_TextAreaID_22
):now, in Dashboard, execute this function (note, using of
My_TextAreaID_22
andMy_Action_Name
):You need to call the editor init again once you add your ajax textarea, I did it like this:
Then call your function after your ajax, like this:
The the usable solution from @toscho on github.
He build this nice result also for a question here, see his answer for more details.
I managed it in this way:
Id must be rundom and unique. Settings must be the same as the settings in your ajax editor.
wp_editor( '', '[set id as you need]', array(the same settings as in the main page) );
_WP_Editors::editor_js(); //this print editor init code
This will work on admin pages.
To append a new wp editor to a container by JS AJAX:
1) Create an wp_ajax function in functions.php to return the wp_editor
2) Create a jQuery script to request a new text editor and append it to a container, for this case, when pressing a button
PHP File
JS Script (jsfile.js)
Enqueue scripts call:
Use this code, hope it will help:
More detail can be found here.