-
-
Save manidip/82ba338efc842acfb3d4b13b2cf88cec 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
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 | |
| /** | |
| * 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 my_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' => '—', | |
| 'class' => 'widefat' | |
| ) ); | |
| } | |
| ?> | |
| </div> | |
| <?php | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment