Last active
June 10, 2020 16:45
-
-
Save thierrypigot/1f2c35baa2a361b1a8c295988852af69 to your computer and use it in GitHub Desktop.
WordPress: Register taxonomy hack WordPress. In the register_taxonomy function, add : $args = array( 'meta_box_cb' => 'tp_taxonomy_radio_meta_box' );
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 | |
/** | |
* Display taxonomy selection as radios | |
* | |
* @param WP_Post $post | |
* @param array $box | |
*/ | |
if( !function_exists('wearewp_taxonomy_radio_meta_box') ) { | |
function wearewp_taxonomy_radio_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="radio" 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 checkboxes | |
* | |
* @param WP_Post $post | |
* @param array $box | |
*/ | |
if( !function_exists('wearewp_taxonomy_checkbox_meta_box') ) { | |
function wearewp_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 | |
*/ | |
if( !function_exists('wearewp_taxonomy_select_meta_box') ) { | |
function wearewp_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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment