Skip to content

Instantly share code, notes, and snippets.

@richjenks
Created December 15, 2016 17:00
Show Gist options
  • Save richjenks/8353b0e43d339ff931743f05c716c1a4 to your computer and use it in GitHub Desktop.
Save richjenks/8353b0e43d339ff931743f05c716c1a4 to your computer and use it in GitHub Desktop.
Changes the taxonomy checkboxes into radios so only one can be selected
<?php
/**
* Changes the taxonomy checkboxes into radios so only one can be selected
*
* @param string|array $taxonomies Taxonomy(s) to be affected
* @param string|array $post_types Post Type(s) to be affected
*/
function taxonomy_radios( $taxonomies, $post_types ) {
// Ensure params are arrays
if ( !is_array( $taxonomies ) ) $taxonomies = [ $taxonomies ];
if ( !is_array( $post_types ) ) $post_types = [ $post_types ];
// Loop through post types
foreach ( $post_types as $post_type ) {
// If correct post type, add action to output script
if ( Posts::get_post_type() === $post_type ) {
add_action( 'admin_footer', function() use ( $taxonomies ) {
echo '<script>';
foreach ( $taxonomies as $taxonomy ) {
echo 'jQuery("[id^=in-'.$taxonomy.']").attr("type", "radio");';
echo 'jQuery("#'.$taxonomy.'-tabs").hide();';
}
echo '</script>';
} );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment