Changing Top Level Items into Radio Buttons in the Categories Meta Box?

I know it is possible to change the WordPress category list to radio buttons in the admin panel for posts, but I would like to make the just the Parents radio buttons and the sub categories or children checkboxs… something like this.

<ul>
    <li><input type="radio"> Category 1
        <ul class="children">
        <li><input type="checkbox">Category 1 - Sub Cat 1</label></li>
        <li><input type="checkbox">Category 1 - Sub Cat 2</label></li>
        </ul>
    </li>
    <li><input type="radio"> Category 2</li>
    <li><input type="radio"> Category 3</li>
    <li><input type="radio"> Category 4
        <ul class="children">
        <li><input type="checkbox">Category 4 - Sub Cat 1</label></li>
        <li><input type="checkbox">Category 4 - Sub Cat 2</label></li>
        </ul>
    </li>
    <li><input type="radio"> Category 5</li>
</ul>

I don’t really want to mess around with the core files and wondered if anyone knows a way to override the output via the theme functions or another method.

2 Answers
2

I’m pretty sure you could use JavaScript to do this for you very simply. I’d try this out:

function convert_root_cats_to_radio()
{
    global $post_type;

    if ( 'post' != $post_type )
        return;
    ?>
    <script type="text/javascript">
    jQuery("#categorychecklist>li>label input").each(function(){
        this.type="radio";
    });
    </script>
    <?php
}
add_action( 'admin_footer-post.php', 'convert_root_cats_to_radio' );
add_action( 'admin_footer-post-new.php', 'convert_root_cats_to_radio' );

What this does is loops through the hierarchy and converts the top level items to radio buttons. If you need to strictly enforce this, the javascript won’t be a good solution because the root-level categories can still be selected under the popular tab.

Check out this plugin, too, to keep the hierarchy after saving: http://wordpress.org/extend/plugins/category-checklist-tree/

If you need something a little more enforceable, you’ll need a custom plugin to that. The above mentioned plugin would event be a great place to start in that endeavor!

Cheers~

Leave a Comment