Radio Categories

By Michael Fields

This plugin forces your writers to only use one category for each post by enabling the category meta box to use radio buttons instead of check boxes.

Ever wanted to restrict category usage to one category per post? I have. and so have others. Here is an easy to use plugin which will replace the default category checklist to one that uses radio buttons. This change will force all writers on your blog to use only one category per post.

It is quite possible that, if you are installing this plugin on an already existing site, you may have posts that contain more than one category. Since radio buttons allow only one option to be selected at a time, these posts will look like only one category has been assigned while in actuality, there are many. I have created the 1 Post/Category utility which can be accessed via the Tools section of the Administration Menu. Please use this tool to ensure that your posts are in the correct category before allowing other authors to post.

Please note that this plugin only changes the meta box under “Edit”. The “Quick Edit” menu will still use check boxes. I plan on updating this in a future release.

Download

Latest version: Download Radio Categories v0.2 [zip]

Installation

  1. Download
  2. Unzip the package and upload to your /wp-content/plugins/ directory.
  3. Log into WordPress and navigate to the “Plugins” panel.
  4. Activate the plugin.

Changelog

0.2

  • Now works on WordPress version 2.9.2
  • Added support for + “Add New Category”.
  • CSS updates for hierarchical category display.
  • Moved the 1 Post/Category utility into it’s own file.

0.1

  • Original Release – Works on WordPress version 3.0 Beta 2.

18 Comments Leave a comment

  1. Eliza May 14, 2010 at 8:35 pm

    Thanks for this great public. I seem to having some difficulty though. Get the following error message

    “Fatal error: Call to undefined function get_admin_url() in C:\xampp\htdocs\wordpress\wp-content\plugins\radio-button-categories\radio-button-categories.php on line 207″

  2. Michael Fields May 15, 2010 at 4:53 am

    Eliza, No problem – there were a couple of bugs that I need to work out. Sorry about that. Should be working smoothly tomorrow morning.

  3. Michael Fields May 15, 2010 at 11:36 am

    I think I got all of the bugs worked out. Please give it a try and let me know how it works for you.

  4. Mac_Boy May 15, 2010 at 6:08 pm

    Can you make a ‘menu” version?

    Single select and multi-select

    Please.

    :-)

  5. Eliza May 15, 2010 at 11:17 pm

    Thank you! That worked a treat. I am in your debt :D

  6. shawn May 18, 2010 at 7:45 pm

    First of all, thank you for yet another great plugin.

    I’ve been dissecting the code all morning in an attempt to turn it into a theme function, but have not quite made it work. The reason I want it as a theme function, is that instead of using it for categories, I wanted to use this code for a custom taxonomy that I create.

    Scenario:
    I create custom hierarchical taxonomy ‘director’. In the post edit screen, instead of having the standard category choices display, I want to use the radio box display so that the author is restricted to only applying one director to the movie.

    Of course, expanding on my other idea using taxonomy images. If the director does not exist in the radio dropdown, give the author the ability to add a director including the directors image/description, while on the create/edit movie admin page.

    -Really the more I think about it, the more I can see how this could be a great new plugin/function. Giving the admin the ability to choose the type of taxonomy display on the admin screen, including ability to input new fields like image into the taxonomy if they don’t exist.

    basically in my theme functions file, when I am creating a new taxonomy and assigning it to a post_type, have the ability to choose the admin display type, (radio, checkbox, menu, etc). And also have the ability to add fields like tax image while on the post create/edit admin screens. Now that would be cool.

  7. Maor Barazany July 26, 2010 at 9:32 am

    Thanks for your plugin.
    I want to restrict to radio also in the quick edit panel.
    How can I implement that? Is there a WordPress filter/action that deals with the quick edit and there I can hook your function?

    Thanks

  8. Michael Fields July 26, 2010 at 11:01 pm

    Maor,
    Your welcome, glad it’s of some use for you. While I was coding it, I really couldn’t find a way to hook into the quick edit screen to do exactly what I needed to do. Will have to revisit this in the future. If you find a solution, please feel free to post back and I will update.

  9. Maor Barazany July 26, 2010 at 11:40 pm

    Hey Michael,

    I found a very quick solution to that – uses jQuery to manipulate the input fields and covert them to radio buttons instead of checkboxes. Works great in the quick edit mode, as well as in full post edit mode.


    add_action( 'admin_footer', 'catlist2radio' );
    function catlist2radio(){
    echo '';
    echo 'jQuery("#categorychecklist input, #categorychecklist-pop input, .cat-checklist input")';
    echo '.each(function(){this.type="radio"});';
    }

    That’s it, and it’s doing all the job :)

  10. Maor Barazany July 26, 2010 at 11:43 pm

    in the first echo there is script type = text / javascript

    and ofcourse closing tag for script in the end..
    It has been disappeared after posting it, for reasonable reasons (why let inject js in the comments…. the code attribute didn’t help here, but I assume you will understand the missing code)

  11. Michael Fields August 1, 2010 at 12:03 am

    Maor Barazany,
    Seems like a simple enough solution, If you don’t mind I would like to work this into the next release to fix the quick edit boxes. I think that I’ll leave the php solution in place for the Add/Edit posts screen though. in case the administrator has disabled javascript for one reason or another, it will revert back to checkboxes if using javascript alone. Quick edit is hidden for users with no javascript so I think that using your solution there is a wonderful way to fix this issue. Thanks!

  12. Maor Barazany August 2, 2010 at 11:22 am

    Of course you may use this code for your plugin. :)

  13. Michael Fields August 23, 2010 at 8:00 pm

    I attempted to integrate the javascript that you posted above but ran into a snag and wondered how you addressed it. The code did in fact change the check boxes to radio buttons for the quick edit sections but it does not preserve the checked items in each set. I did a little digging and it looks like WordPress does not define the checked elements in the generated html – they are dynamically defined via javascript. Did you run into this issue? If so, what did you do to fix it?

  14. Israel Curtis August 28, 2010 at 7:20 am

    I’ve run into the same dead end. Radio buttons appear, but no current values. Makes this a novel solution, but not a working one. Someone better at javascript than I might be able to tweak it – check out wp-admin/js/inline-edit-post.dev.js on line 149. Not sure how we’d hook in, would probably have to simply override the existing jquery…

    I’ve got quite a few custom taxonomies, and the result is a really tall quick-edit box with all these checkbox fields. I’d be even happier if I could transform them to drop-down select inputs – saving a lot of screen space and restricting choice to only one at the same time…

  15. Marcus March 9, 2011 at 7:42 pm

    nice plugin, worked out the box for post categories.

    why don’t you put this up on the WP repo? more people will find it, as well as making it easier for others to svn and patch/improve things.

  16. Michael Fields March 10, 2011 at 8:48 pm

    Thanks! Glad it worked for you. It is actually hosted on WordPress.org
    check the download link above :)

  17. Marcus March 10, 2011 at 11:06 pm

    how weird, don’t know what search I used but normally wp plugins always come out on top first! I just assumed it wasn’t after that… thx for pointing that out :)

  18. Igor December 2, 2011 at 8:03 am

    Thank you Michael for plugin and Maor for javascript, but this solution doesn’t work for me too (there are no selected items in the row after changing of type). Did you solve it anyhow?

Share your thoughts

*

Fork me on GitHub