Adding rich text editor, like the one used for the post editor, was a really big problem in WordPress. And while there were some ways to do it, it was never working as you would expect it, there were too many issues and conflicts. But, with WordPress 3.3, all that changes.
In my earlier article on the subject (Add rich text editor to your plugin), I have described a method on how to add single, fully working rich text editor to any plugin. And, for that specific purpose, that method was working great. But, if you needed 2 editors on the same page, or if you needed to add editor in the meta box on the post editor page, it wasn’t working. There are other methods on the internet to do this, but none of the is actually fully working: all involve some hacking, and all were messy to use.
Now, in WordPress 3.3 there is a new way of doing this. Whole editor code is updated to allow more than one editor instance on any admin screen (still haven’t had a chance to see how to make this work on the front end), including post editor page. And, all that is needed is one single function:
wp_editor($content, $editor_id, $settings = array());
First value is actual content you want to display in editor when it is displayed. Second one is very important, and this needs to be unique since it is used as an ID for the editor control. Last value can be skipped, but it can be used to add more settings to the editor. There are many settings you can add with the last parameter, and some of them are not working now (WP 3.3 Beta 1). Here are some of the settings you will need and use:
- wpautop: enable the default WordPress content formatting, active by default.
- media_buttons: show or hide upload insert button, active by default.
- textarea_name: name for the editor element. By default it is same as $editor_id you pass to the function. This is name used to identify the editor in the $_POST for saving purpose.
- textarea_rows: number of rows, size of the editor.
- editor_class: extra CSS class to add to the editor.
All the other settings can be found in WP_Editor class, for editor method. So typical example of how to use this would be:
$args = array("textarea_rows" => 5, "textarea_name" => "editor_content_1", "editor_class" => "my_editor_custom"); wp_editor("My editor content", "my_editor_1", $args);
And that is all you need. You can play with settings to see how this works. New editor model is done really well, and so far in the current Beta works pretty much as expected. There will be some changes until final 3.3 is released, so check this post out for all the changes in the editor implementation.