Skip to content

Instantly share code, notes, and snippets.

@obiPlabon
Last active July 3, 2018 13:16
Show Gist options
  • Save obiPlabon/93e9d8968996c96edb5b4e7acf9176ab to your computer and use it in GitHub Desktop.
Save obiPlabon/93e9d8968996c96edb5b4e7acf9176ab to your computer and use it in GitHub Desktop.
WP taxonomy metabox callback to generate radio selection
<?php
/**
* Override the default taxonomy metabox
*
* Generate a taxonomy meta box with radio button
*/
function op_taxonomy_meta_box_cb( $post ) {
$_writers = get_the_terms( $post->ID, 'writer' );
$selected_writers = array();
if ( ! empty( $_writers ) || ! is_wp_error( $_writers ) ) {
foreach ( $_writers as $_writer ) {
array_push( $selected_writers, $_writer->term_id );
}
}
$writers = get_terms( array(
'taxonomy' => 'writer',
'hide_empty' => false
) );
echo '<ul class="categorychecklist form-no-clear">';
if ( ! empty( $writers ) || ! is_wp_error( $writers ) ) {
foreach ( $writers as $writer ) {
$selected = false;
if ( in_array( $writer->term_id, $selected_writers ) ) {
$selected = true;
}
printf( '<li><label><input type="radio" name="tax_input[writer][]" value="%1$s" %3$s>%2$s</label></li>',
$writer->term_id,
$writer->name,
$selected ? 'checked="checked"' : ''
);
}
}
echo '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment