Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save elvishp2006/9cc42832019ba9a8d04aaf1f38d3615b to your computer and use it in GitHub Desktop.
Save elvishp2006/9cc42832019ba9a8d04aaf1f38d3615b to your computer and use it in GitHub Desktop.
Callback function for 'meta_box_cb' argument inside register_taxonomy() that replaces the regular checkboxes with a plain dropdown list
<?php
/**
* Callback function for taxonomy meta boxes
*
* A simple callback function for 'meta_box_cb' argument
* inside register_taxonomy() that replaces the regular
* checkboxes with a plain dropdown list
*
* @param [type] $post [description]
* @param [type] $box [description]
* @link http://wordpress.stackexchange.com/a/148965
*/
function dropdown_taxonomy_meta_box_callback( $post, $box ) {
$defaults = array( 'taxonomy' => 'category' );
if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
$args = array();
}
else {
$args = $box['args'];
}
extract( wp_parse_args($args, $defaults), EXTR_SKIP );
$tax = get_taxonomy( $taxonomy );
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
<?php
$name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
$term_obj = wp_get_object_terms( $post->ID, $taxonomy ); //_log($term_obj[0]->term_id)
if ( ! empty( $term_obj ) ) {
wp_dropdown_categories( array(
'taxonomy' => $taxonomy,
'hide_empty' => 0,
'name' => "{$name}[]",
'selected' => $term_obj[0]->term_id,
'orderby' => 'name',
'hierarchical' => 0,
'show_option_none' => '&mdash;',
'class' => 'widefat'
) );
}
?>
</div>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment