Created
June 24, 2014 16:41
-
-
Save Ciantic/8a09107fcd9bed8bede0 to your computer and use it in GitHub Desktop.
WordPress select and checkboxes metabox callback
This file contains 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 | |
/** | |
* Show taxonomy selection in WordPress admin as single <select> box or checkboxes | |
* | |
* @author Jari Pennanen / https://github.com/ciantic | |
* @license Public Domain | |
**/ | |
// Usage example: | |
register_taxonomy("my_taxonomy", array("post"), array( | |
// ... | |
// single select box | |
"meta_box_cb" => 'taxonomy_select_meta_box', | |
// list of checkboxes (no "Add categories" possibility) | |
"meta_box_cb" => 'taxonomy_checkbox_meta_box', | |
)); | |
/** | |
* Display taxonomy selection as checkboxes | |
* | |
* @param WP_Post $post | |
* @param array $box | |
*/ | |
function taxonomy_checkbox_meta_box($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); | |
$selected = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids')); | |
$hierarchical = $tax->hierarchical; | |
?> | |
<div id="taxonomy-<?php echo $taxonomy; ?>" class="checkdiv"> | |
<?php if (current_user_can($tax->cap->edit_terms)): ?> | |
<input type="hidden" name="tax_input[<?php echo $taxonomy ?>][]" value="0" /> | |
<ul class="categorychecklist"> | |
<?php foreach (get_terms($taxonomy, array('hide_empty' => false)) as $term): | |
$value = $hierarchical ? $term->term_id : $term->slug; | |
?> | |
<li> | |
<label class="selectit"> | |
<input type="checkbox" name="<?php echo "tax_input[$taxonomy][]"; ?>" | |
value="<?php echo esc_attr($value); ?>" <?php checked(in_array($term->term_id, $selected)); ?> /> | |
<?php echo esc_html($term->name); ?> | |
</label> | |
</li> | |
<?php endforeach; ?> | |
</ul> | |
<?php endif; ?> | |
</div> | |
<?php | |
} | |
/** | |
* Display taxonomy selection as select box | |
* | |
* @param WP_Post $post | |
* @param array $box | |
*/ | |
function taxonomy_select_meta_box($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); | |
$selected = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids')); | |
$hierarchical = $tax->hierarchical; | |
?> | |
<div id="taxonomy-<?php echo $taxonomy; ?>" class="selectdiv"> | |
<?php if (current_user_can($tax->cap->edit_terms)): ?> | |
<?php | |
if ($hierarchical) { | |
wp_dropdown_categories(array( | |
'taxonomy' => $taxonomy, | |
'class' => 'widefat', | |
'hide_empty' => 0, | |
'name' => "tax_input[$taxonomy][]", | |
'selected' => count($selected) >= 1 ? $selected[0] : '', | |
'orderby' => 'name', | |
'hierarchical' => 1, | |
'show_option_all' => " " | |
)); | |
} else { | |
?> | |
<select name="<?php echo "tax_input[$taxonomy][]"; ?>" class="widefat"> | |
<option value="0"></option> | |
<?php foreach (get_terms($taxonomy, array('hide_empty' => false)) as $term): ?> | |
<option value="<?php echo esc_attr($term->slug); ?>" <?php echo selected($term->term_id, count($selected) >= 1 ? $selected[0] : ''); ?>><?php echo esc_html($term->name); ?></option> | |
<?php endforeach; ?> | |
</select> | |
<?php | |
} | |
?> | |
<?php endif; ?> | |
</div> | |
<?php | |
} |
Oh, bless you!
Miles better than all of the other select meta box callbacks I've seen. Others were causing some pretty strange behaviour because they didn't account for non-hierarchical taxonomies. Thanks for sharing.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice job ;)