Using Chosen for a replacement taxonomy metabox

If you’ve ever created many taxonomies for a post type, you know that the add/edit screen can get very unwieldy with all of those metaboxes, especially with hierarchical taxonomies. And then sometimes you have something that is a taxonomy but doesn’t need items added very often, especially in client work where there are often predetermined options that may occasionally be added to by the client, which can be done in the taxonomy’s admin screen.

This is where we can use Chosen, a JS plugin to make multi-selects much more user-friendly, and just so happens to blend right in to the WordPress admin. You can take multiple taxonomies and put them into a single metabox. It’s not limited to taxonomies, either – you can use it for post meta (custom fields) as well. I’ve used it to select taxonomy terms for which a post is “essential” – optgroups came in very handy there. I actually would love to see tag selection look and behave more like this in core, but there would be more work to be done for adding new terms.

The basic idea is that you enqueue the script and style on the appropriate admin page (NOT all admin pages) and then initialize the field(s) based on a class or ID. I usually just put the initialize right in the metabox itself, but you could always be clean and put it in the admin_head if you want. From there, you show all terms in the taxonomy in a select field (including empties) and use the nifty selected() function to show which ones are associated with the current post. When saving, it’s as easy as wp_set_post_terms() after the usual checks for nonce, etc. You’ll also want to hide the original taxonomy metabox(es) as well, of course.

Full Github Gist (updated July 10, 2012):

If you see anything that could use improving, let me know!


15 comments

    • Somehow I missed a ton of comments – sorry about that! If you’re doing this in the theme, you can put the code as-is into functions.php (or, I’d probably actually recommend putting it into a separate file that you include in functions.php) and then put the Chosen folder in a js folder in the theme. With Chosen, you’ll only need the jQuery minified version of the Javascript plus the CSS and images. If you put it somewhere else, just change the enqueue call. If you do this in a plugin, you’ll want to use `plugins_url()` instead of `get_template_directory_uri()`. Similarly, a child theme will need `get_stylesheet_directory_uri()` instead of the get_template version.

  1. Hi, Helen.

    I love this concept, and I’ve installed per your comment. Nothing happening in the back-end yet.

    So, when included into a theme’s functions.php file, do the individual taxonomies have to be included in this code, or does it pick them all up into a single meta box?

    • You’ll need to specify which taxonomies are used in the `$taxes` array, which are then put into a single metabox. There may be some other things that need tweaking here and there depending on how and where you’re using the code – I haven’t done a ton of testing in various use cases. Are you getting any errors or is just nothing happening at all?

  2. Since my coding expertise is primarily copy and paste, I’ll be spending some time with our developer this afternoon to better understand the code and answer your questions. This is very promising!

  3. Hi Helen,

    Thanks for the great info! I just wrote about styling forms to match Gravity Forms (enhanced using Chosen jQuery), but never thought about using it for the admin.

    What a great idea…I’m gonna try it out and see how it works.

    Thanks again!

    Tony

  4. Your enthusiasm on make.wordpress.org is infectious. :-)

    We’ll include this Chosen on a feature release of our own plugin.

    Thx for sharing.

  5. Very nice code. Used in my site but it needs improvements like:
    #1 I can only add terms and I should be able to remove them too.
    #2 does not work with tags ( should use $term->slug; for tags )
    #3 the IFs in the hhs_add_admin_scripts function seem not to work.
    #4 when I edit the post type I see “Select one or more” even if there are selected taxonomies from a previous edit

  6. Pingback: An online community for educators built with BuddyPress · BuddyPress.org

  7. Pingback: BuddyPress: An online community for educators built with BuddyPress | WordPress Planet

  8. Pingback: An online community for educators built with BuddyPress | DWH-UK.com : Great Themes at Sensibles Prices!

  9. A little bit of knowledge goes a long way in all situations in life.
    Buying a car is no different! That means you need to read advice from experts, as detailed below, to ensure that when you shop for that car,
    you really know what you’re doing and how to get the best deal.

    • Chosen is pretty simple – you can’t have “Add New” on the post edit screen. You can always go to the taxonomy’s edit screen to add terms. I think Select 2 can do “Add New” behavior, although you’d probably have to do some magic to make it work with hierarchical taxonomies. I would definitely say that this is geared toward fixed or rarely-changing sets of terms.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s