Last active
February 27, 2017 01:03
-
-
Save 5A5K1A/f442b165952173c6e0cd8f85f9414409 to your computer and use it in GitHub Desktop.
WordPress Change custom taxonomy metabox (checkboxes to radios, in order to force one)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Custom Tax Metabox : remove the current custom taxonomy metabox | |
/* -------------------------------------------------- */ | |
add_action( 'admin_menu', function(){ | |
remove_meta_box('customtaxdiv', 'posttype', 'normal'); | |
}); | |
/* Custom Tax Metabox : add a new & improved custom taxonomy metabox (select-one) | |
/* -------------------------------------------------- */ | |
add_action( 'add_meta_boxes', function() { | |
add_meta_box( 'customtaxdiv_id', 'Title metabox', 'customtax_metabox', 'posttype', 'side', 'core'); | |
}); | |
function customtax_metabox( $post ) { | |
//Get taxonomy and terms | |
$taxonomy = 'customtax'; | |
//Set up the taxonomy object and get terms | |
$tax = get_taxonomy($taxonomy); | |
$terms = get_terms($taxonomy,array('hide_empty' => 0)); | |
//Name of the form | |
$name = 'tax_input[' . $taxonomy . ']'; | |
//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 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 = $taxonomy.'-'.$term->term_id; | |
echo "<li id='$id'><label class='selectit'>"; | |
echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />"; | |
echo "</label></li>"; | |
}?> | |
</ul> | |
</div> | |
<!-- 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 = 'popular-'.$taxonomy.'-'.$term->term_id; | |
echo "<li id='$id'><label class='selectit'>"; | |
echo "<input type='radio' id='in-$id'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />"; | |
echo "</label></li>"; | |
}?> | |
</ul> | |
</div> | |
</div> | |
<?php | |
} | |
/* Custom Tax Metabox : add some JS to keep it functioning right | |
/* -------------------------------------------------- */ | |
add_action('admin_enqueue_scripts', function() { | |
wp_register_script( 'radiotax', get_template_directory_uri() . '/js/radiotax.js', array('jquery'), null, true ); | |
// We specify true here to tell WordPress this script needs to be loaded in the footer | |
wp_enqueue_script( 'radiotax' ); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($) { | |
var taxonomy = 'customtax'; | |
$('#' + taxonomy + 'checklist li :radio, #' + taxonomy + 'checklist-pop :radio').live( 'click', function(){ | |
var t = $(this), c = t.is(':checked'), id = t.val(); | |
$('#' + taxonomy + 'checklist li :radio, #' + taxonomy + 'checklist-pop :radio').prop('checked',false); | |
$('#in-' + taxonomy + '-' + id + ', #in-popular-' + taxonomy + '-' + id).prop( 'checked', c ); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment