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.
- Install Plugin
- Activate Plugin
- Go to Settings>Radio Buttons for Taxonomies
- 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.
or you can track, contribute to the development, or fork me at at Github.