Add rich text editor(s)… the right way

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.

Two editors on the same screen

Two editors on the same screen

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.

16 Responses to “Add rich text editor(s)… the right way”

  1. Edward Caissie | October 20, 2011 at 17:15

    I think you are missing a quote in your code “example” but otherwise this is a great tip … just have to think where I might want to apply it.

    VA:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • MillaN | October 20, 2011 at 19:30

      Thanks, I have fixed that.

      VN:R_U [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)
  2. shawn | October 25, 2011 at 12:51

    Here’s a bugger one to figure out when using wp_editor.

    Using the editor on bbPress, and it works perfectly when creating new topics, however when creating a ‘reply’ to a topic, the html editor looses its buttons.

    example reply: (click html not working)
    http://www.bbconverter.com/forums/topic/welcome-to-bbconverter/

    example new topic: (html works)
    http://www.bbconverter.com/forums/forum/announcements/

    The code used to call the editor is identical, so it’s very strange.

    *there are a few other annoyances, but overall it works pretty well for a beta

    VN:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • MillaN | October 25, 2011 at 12:58

      On the page where HTML is not working you have several JavaScript errors, so that’s the problem. Fix them, and it will work. Use Firebug to see the errors.

      VN:R_U [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)
  3. shawn | October 25, 2011 at 14:37

    thnx MillaN

    Yeah, those 2 js errors are on known by JJ and are on the trac. Something with 3.3 doesn’t like bbpress much yet, will track it down.

    VN:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  4. shawn | October 31, 2011 at 22:11

    My firebug is no longer showing me any js errors on the page I linked above but I still don’t see the buttons in html mode.

    Are you able to see something that I am missing?

    I’m so close to getting this all sorted out :)

    VN:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • MillaN | October 31, 2011 at 22:33

      Sorry, until WP is sorted out and final 3.3 is released, I am not sure that it can be fixed.

      VN:R_U [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)
      • shawn | October 31, 2011 at 23:02

        thanks for taking a peek. Was driving me nuts in that it works perfectly on local dev box but not this site. Still pretty cool function though.. 3.3 is gonna be awesome.

        VN:R_U [1.9.17_1161]
        Rating: 0.0/5 (0 votes cast)
        • MillaN | October 31, 2011 at 23:06

          I will be doing some WP 3.3 experiments later this week, and I will have more info if this can be solved soon.

          VN:R_U [1.9.17_1161]
          Rating: 0.0/5 (0 votes cast)
  5. shel | November 22, 2011 at 03:50

    thanks Millan wp 3.3 now makes it so simple with to include the tinymce on frontend pages. Here is another related post outlining some more options for wp_editor()

    http://soderlind.no/archives/2011/09/25/front-end-editor-in-wordpress-3-3/

    VA:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • MillaN | November 22, 2011 at 21:39

      Thanks for the link.

      VN:R_U [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)
  6. fiuz | December 16, 2011 at 19:06

    wow tnx, im just beginning with wp and theme options, and this way is so easy.

    but when i save the data, the quotes are escaped, and when i show data i see the escapes \” , even in the same editor. after looking for a solution for a while, i decided to go with the easy way (on the admin options page and frond-end ):

    str_replace ( ‘\”‘, ‘”‘, get_option(” my_option”) );

    now all works fine O_o’

    VA:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
  7. Taeke | February 9, 2012 at 09:37

    How would you add this to a Gravity Forms textarea?

    VA:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • MillaN | February 9, 2012 at 09:39

      Right now I am don’t think you can. Gravity Forms needs to change the way they render textarea to use new editor function. Until that happens, new API can’t help you.

      VN:R_U [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)
  8. bummer | February 26, 2012 at 17:08

    Great function that I have missed. I cannot figure out how would I add this to let’s say pages.

    Does it have to be wrapped in a function or something (that will call it below the default editor)?

    VA:R_U [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Trackbacks/Pingbacks

  1. Add rich text editor(s)… the right way – Tips | Dev4Press » Web Design - February 17, 2012

    [...] Add rich text editor(s)… the right way – Tips | Dev4Press [...]

Leave a Reply

Follow us on Twitter

Social Networks

Google Plus Profile Subscribe to RSS feed LinkedIn Profile Follow me on Pinterest Watch our Youtube Videos Find us on Flickr

Feedburner Feedburner updates

Sign up to receive news from this website to your email.

Recent Blog Posts

Dev4Press on YouTube

Archives