Skip to content

Instantly share code, notes, and snippets.

@stevetrask
Created February 25, 2016 22:39
Show Gist options
  • Save stevetrask/1b2b918d804e292007af to your computer and use it in GitHub Desktop.
Save stevetrask/1b2b918d804e292007af to your computer and use it in GitHub Desktop.
/* To use radio inputs instead of checkboxes for `product_types` taxonomy in add new product page */
function wpse_139269_term_radio_checklist( $args ) {
if ( ! empty( $args['taxonomy'] ) && $args['taxonomy'] === 'product_types' /* <== Change to your required taxonomy */ ) {
if ( empty( $args['walker'] ) || is_a( $args['walker'], 'Walker' ) ) { // Don't override 3rd party walkers.
if ( ! class_exists( 'WPSE_139269_Walker_Category_Radio_Checklist' ) ) {
/**
* Custom walker for switching checkbox inputs to radio.
*
* @see Walker_Category_Checklist
*/
class WPSE_139269_Walker_Category_Radio_Checklist extends Walker_Category_Checklist {
function walk( $elements, $max_depth, $args = array() ) {
$output = parent::walk( $elements, $max_depth, $args );
$output = str_replace(
array( 'type="checkbox"', "type='checkbox'" ),
array( 'type="radio"', "type='radio'" ),
$output
);
return $output;
}
}
}
$args['walker'] = new WPSE_139269_Walker_Category_Radio_Checklist;
}
}
return $args;
}
add_filter( 'wp_terms_checklist_args', 'wpse_139269_term_radio_checklist' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment