How To Change Custom Taxonomy To Radio Buttons

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.

Read More
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.

Related posts

Leave a Reply

2 comments

  1. 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();