• Hello
    I’v made some code that allows to make radio buttons on custom taxanomies select (by default they are checkbox).
    So if enyone need that or looking how to do that her’s my code

    <?php
    /**
     * Custom walker for Walker_Category_Checklist
    **/
    class M7_Walker_Category_Checklist extends Walker {
    	var $tree_type = 'category';
    	var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
    
    	function start_lvl( &$output, $depth = 0, $args = array() ) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent<ul class='children'>\n";
    	}
    
    	function end_lvl( &$output, $depth = 0, $args = array() ) {
    		$indent = str_repeat("\t", $depth);
    		$output .= "$indent</ul>\n";
    	}
    
    	function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
    		extract($args);
    		if ( empty($taxonomy) )
    			$taxonomy = 'category';
    
    		if ( $taxonomy == 'category' )
    			$name = 'post_category';
    		else
    			$name = 'tax_input['.$taxonomy.']';
    
    		$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
    		$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="radio" name="'.$name.'" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
    	}
    
    	function end_el( &$output, $category, $depth = 0, $args = array() ) {
    		$output .= "</li>\n";
    	}
    }
    
    /**
     * Adding our walker to taxonomies
    **/
    function m7_taxonomy_radio( $args, $post_id ) {
        $taxanomies = array('status','types');
        if ( isset( $args['taxonomy'] ) && in_array( $args['taxonomy'], $taxanomies ) )
            $args['walker'] = new M7_Walker_Category_Checklist;
    
    	return $args;
    }
    add_filter( 'wp_terms_checklist_args', 'm7_taxonomy_radio', 10, 2 );
    
    /**
     * Removing navbar tabs with "All #tax_name#" and "Most Used", becouse "Most used" still will be checkbox
    **/
    function m7_admin_head() {
        $taxanomies = array('status','types');
        if($taxanomies){
            echo '<style>';
            foreach($taxanomies as $tax){
                echo 'ul#'.$tax.'-tabs { display: none !important; }
                ';
            }
            echo '</style>';
        }
    }
    add_action('admin_head', 'm7_admin_head');

    I have taken walker that is usecd by default and just changed in function start_el type to radio and in name remover [].

    And anothere thing – your custom taxanomie must be ‘hierarchical’ => true (or not, haven’t yet tested it with hierarchical => false).

    And don’t forget to write your taxanomies names to array –
    $taxanomies = array(‘#tax_name#’);

    Enjoy )

Viewing 1 replies (of 1 total)
  • Thread Starter Mihail Semjonov

    (@muxahuk1214)

    Here’s the update to prevent users from not selecting eny of custom taxanomie

    function unit_admin_head() {
        $taxanomies = array('unit_status','unit_types');
        if($taxanomies){
            echo '<style>';
            foreach($taxanomies as $tax){
                echo 'ul#'.$tax.'-tabs { display: none !important; }
                ';
            }
            echo '</style>';
            echo "<script type='text/javascript'>\n";
            echo "jQuery(function($){\n";
            foreach($taxanomies as $tax){
                echo '
                var $'.$tax.' = $("#'.$tax.'checklist").find(\'input[type="radio"]\');
                if($'.$tax.'.length > 0){
                    var checked_'.$tax.' = 0;
                    $'.$tax.'.each(function(){
                        if($(this).attr("checked") == "checked"){
                            checked_'.$tax.'++;
                        }
                    });
                    if(checked_'.$tax.' == 0){
                        $'.$tax.'.first().attr("checked","checked");
                    }
                }';
            }
            echo "\n});";
        	echo "\n</script>";
        }
    }
    add_action('admin_head', 'unit_admin_head');

    Not a realy good js code, but works..

Viewing 1 replies (of 1 total)
  • The topic ‘Single category select’ is closed to new replies.