Skip to content

Radio Buttons for Taxonomies

Radio Buttons for Taxonomies!

Radio Buttons for Taxonomies! Unfortunately the subject can no longer be both guacamole and bacon. You’ll have to chose.

Every now and then I run across a client who needs a taxonomy where the user is restricted to only allowing one term per post.  I’ve also seen people wanting this a few times for categories, and I, myself, have wished for it a few times.  Sometimes a post needs to be like a piece of paper in a folder… and only be in 1 dang folder at a time.  Some might argue that categorization with a single, exclusive term is more-suited to post meta, and they might be right, but the ability to list all the existing terms is lacking from a post meta implementation.

So, the problem is checkboxes… checkboxes let users check more than one box.  So, the solution is then radio buttons!  Which enforce a single selection.  There are a few tutorials floating around on the interwebs on how to do this.

Hands-down, the best tutorial on radio buttons with taxonomies was written by Stephen Harris, so I owe him a huge debt for providing most of the guts of my plugin.  His OOP class implementation allowed you to define some variables, such as taxonomy and post type… and the class would do the rest, but only for a single taxonomy.  Well the whole point of OOP-style programming is that you can re-use it!  I set about building a standard plugin container with a settings page where you can check which taxonomies you’d like to convert to radio buttons.  I would then create a new object based on each of these taxonomies.  So I had to tweak Stephen’s class so that I could use it to create new objects (basically just switched his load method to __contruct() and pass those objects a parameter, namely the taxonomy.  I scrapped the other parameters.   The div id will just be standardized based on the taxonomy and the metabox will be changed for every post type that uses a particular taxonomy.

Radio Buttons for Taxonomies settings configuration

Now it is super simple to convert any taxonomy to radio buttons

Installation Instructions

  1. Install Plugin
  2. Activate Plugin
  3. Go to Settings>Radio Buttons for Taxonomies
  4. Check which taxonomies you’d like to convert to taxonomies and click Save Changes

Literally, that’s it.  And now when you return to your edit your post you will see the new metabox.  Hope you enjoy!  I have a few ideas for improvements, such as better error handling when you try to add new terms and getting radio buttons in the quick edit, but this should get you started.

Download

Download from the official WordPress Directory

or you can track, contribute to the development, or fork me at at Github.

Posted in

20 Comments

  1. Eben on August 31, 2012 at 5:32 am

    Hey Kathy

    Thanks for this, it works great. I was just about to go through the whole WPtuts tutorial but I read the comments before I do the tutorials and I came across yours, fantastic. 5 stars from me :)

    Thank you



    • kathy on August 31, 2012 at 6:14 am

      You’re welcome! Glad it helped you. I’ve been playing around w/ getting it to work in the quick edit and especially getting term hierarchy back, but those are turning out to be really, really hard.



      • Paul Biron on December 12, 2012 at 11:14 pm

        I just started using this plugin last week and just noticed the problem with quick edit. I poked around in the code and found the following in inc/class.Walker_Category_Radio.php:

        checked( in_array( $term->term_id, $selected_cats ), true, false ). The $selected_cats variable is not set and from the little time I’ve looked at the code I can’t tell where it would be set. Since you know the code much better than I, do you have an clues?



        • kathy on December 13, 2012 at 3:38 am

          Thanks for the heads up, Paul. Would you try using the version currently in github? I made a small patch. Please let me know if that solves your issue and I will get it pushed out to the WP repo when I can.



          • Paul Biron on December 13, 2012 at 7:38 pm

            no, it doesn’t. When I click on Quick Edit I get the following errors (running WP in debug mode):

            Notice: Undefined variable: post in C:\sites\Single53\pgarcia1\webroot\wp\wp-content\plugins\radio-buttons-for-taxonomies\inc\class.Walker_Category_Radio.php on line 38

            p.s. the “Visit plugin site” link in the Plugins page goes to your subtitle plugin instead of this one :-)



          • Paul Biron on December 13, 2012 at 7:42 pm

            p.s. the line # in the error msg above is off by 2…I had added a couple of tracing statements



        • kathy on December 13, 2012 at 8:34 pm

          Wow, I totally need to do something about my nested comments. Please check out the latest in github. If it doesn’t work, please post an issue there so we don’t have to communicate via these busted WP comments.

          You should be able to disable my columns if you like (with code). I don’t like the idea of a theme adding columns… themes should be for front-end display imo, but to each his own. It is going to be a while before I can devote enough time to implement your suggestion.



  2. Murali Kumar on October 3, 2012 at 7:50 am

    Thanks.
    It saved me a lot of time. :)
    Awesome!



  3. franck on November 12, 2012 at 5:40 pm

    Thanks, it’s really usefull.
    A problem I got with it to achieve my task is that it seems it disables the way the original “hierarchical taxonomies” box of wordpress (the one with multiple checkbox) refreshed the post edit page on changes. As it appends, it allows too use the rules provided by the ACF plugin (custom fields appearing or not depending on active terms). Could it be easy to enable it ?



    • kathy on November 12, 2012 at 5:48 pm

      Hi Franck… unfortunately I don’t understand what you are asking. I can’t promise that my plugin will work will be 100% compatible with ACF. Désolée, j’ai pas compris ta question… est c’est pas possible de garantir que mon extension sera compatible avec ACF à 100%.



  4. Paul Biron on December 13, 2012 at 8:19 pm

    Here’s another suggestion: add an option for whether or not to add columns to the edit screen.

    The reason I’m asking for this is that the theme I’m writing and using this plugin for already adds taxonomy columns (whether the taxonomy is managed by this plugin or not)



    • Paul Biron on December 13, 2012 at 10:38 pm

      I understand re: your comment above about themes adding columns to edit.php. The theme I’m writing is for a client and not a general purpose theme that I intend to release to the public. When I’m developing a site for a client, I generally incorporate plugin-like code (whether it affects the front-end or the admin module) into the theme. As you say, to each his own.

      The error caused by $post being undefined is now gone, but the the correct value is not always checked in Quick Edit. When I get a chance (which may not be for a day or two), I’ll dig into your code and comment with what I learn on github.

      Thanx again for your quick responses!



      • kathy on December 13, 2012 at 11:08 pm

        Check out this post: http://ottopress.com/2011/creating-a-site-specific-snippets-plugin/ If it were me, I’d wrap that type of functionality into a site-specific plugin.

        But, do let me know what you find in my code. The quick edit works for me in a cursory test. You might have a problem if you had previously saved more than one category as it will pull off only the first category.



  5. Kevin McGillivray on March 25, 2013 at 1:12 pm

    I just installed this plugin on WordPress 3.5.1 and configured it to apply to two custom taxonomies associated with a single custom post type. When I’m editing a post and click the Add New [ Taxonomy Name] button in one of the meta boxes, the form for adding a new term is displayed but it won’t let me submit the form. There are no error messages when I submit the form and there are no JavaScript errors showing up in the console in Firebug, either. I’m going to post this as an issue on GitHub. Please let me know if you need any other info.



  6. Anvy on May 1, 2013 at 6:33 pm

    Hello,

    Nice Plugin, Is there anyway to display terms as checkboxes so that if we want more than one term per post, we can select more than 1 term.

    I want to use checkboxes instead of radio boxes… any help ?

    thanks



    • kathy on May 1, 2013 at 8:25 pm

      If you don’t want radio buttons then don’t use my plugin on that taxonomy. WordPress uses checkboxes for hierarchical terms (such as categories) by default.



  7. Glark on May 11, 2013 at 8:56 pm

    Hi! I’m using this plugin but hit a snag today. An author added a “feature” (that’s the tax we’re are using) but shouldn’t have. There’s no way to go back to have nothing selected with this plugin that I can see. That would be useful as the tax is question is optional and sometimes posts have it, sometimes they don’t. Thanks!