Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've actually found a solution to my problem here: Altering the appearance of custom taxonomy inputs but I wanted to ask a followup question, and it seems I couldn't ask in that thread.

Here's the code provided on that thread that seemed to work.

add_action('add_meta_boxes','mysite_add_meta_boxes',10,2);
    function mysite_add_meta_boxes($post_type, $post) {
    ob_start();
}
add_action('dbx_post_sidebar','mysite_dbx_post_sidebar');

function mysite_dbx_post_sidebar() {
    $html = ob_get_clean();
    $html = str_replace('"checkbox"','"radio"',$html);
    echo $html;
}

When I use the code above, it does transform the checkboxes in all taxonomy boxes to a radio button. But I only want 3 boxes to change their checkboxes into a radio button. So how do you turn the checkboxes into radio buttons for a specific taxonomy box?

tnx.

share|improve this question

2 Answers 2

The most straightforward way would be to to de-register the taxonomy's metabox and replace it with your own custom metabox. Below is my attempt. There are some drawbacks though, without adding in some javascript, I was unable to replicate WordPress 'add new term' feature, consequently you can only select pre-existing categories. (Or a least I was, but it added a checkbox rather than radio button).

class My_Radio_Tax{

    static $taxonomy = 'event-category'; //Slug of taxonomy
    static $post_type = 'event';//Post type for meta-box

    function load(){
        add_action( 'admin_menu', array(__CLASS__,'remove_meta_box'));
        add_action( 'add_meta_boxes', array(__CLASS__,'add_meta_box'));
    }

    //Remove taxonomy meta box
    function remove_meta_box(){
        //The taxonomy metabox ID. This is different for non-hierarchical taxonomies
        $tax_mb_id = self::$taxonomy.'div';
        remove_meta_box($tax_mb_id, self::$post_type, 'normal');
    }

    //Add new taxonomy meta box
    function add_meta_box() {
        add_meta_box( 'my_tax', 'My taxonomy',array(__CLASS__,'metabox_inner'),'event' ,'side','core');
    }

    //Callback to set up metabox
    function metabox_inner( $post ) {
        //Get taxonomy and terms
        $taxonomy = self::$taxonomy;
        $tax = get_taxonomy($taxonomy);
        $name = 'tax_input[' . $taxonomy . ']';
        $terms = get_terms('event-category',array('hide_empty' => 0));

        //Get current and popular terms
         $popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
        $postterms = get_the_terms( $post->ID,$taxonomy );
        $current = ($postterms ? array_pop($postterms) : false);
        $current = ($current ? $current->term_id : 0);
        ?>
        <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">

            <!-- Display tabs-->
            <ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
                <li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a></li>
                <li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
            </ul>

            <!-- Display popular taxonomy terms -->
            <div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
                <ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
                    <?php   foreach($popular as $term){
                        $id = "id='in-popular-event-category-$term->term_id'";
                        echo "<li id='popular-event-category-$taxonomy-$term->term_id'><label class='selectit'>";
                        echo "<input type='radio' {$id}".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
                        echo "</label></li>";
                    }?>
                </ul>
            </div>
            <!-- Display taxonomy terms -->
            <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
                <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
                    <?php   foreach($terms as $term){
                        $id = "id='in-event-category-$term->term_id'";
                        echo "<li id='event-category-$taxonomy-$term->term_id'><label class='selectit'>";
                        echo "<input type='radio' {$id} name='{name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
                        echo "</label></li>";
                    }?>
                </ul>
            </div>
        </div>
        <?php
    }
}
My_Radio_Tax::load();
share|improve this answer

hope it isn't considered overly self-promotional, but i've turned stephen harris' code into a plugin that will do this for you: http://wordpress.org/extend/plugins/radio-buttons-for-taxonomies/

i'm still working on getting the quick edit to show radio buttons too, but that appears to be significantly harder.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.