Skip to content

Instantly share code, notes, and snippets.

@cr0ybot
Created March 18, 2021 18:46
Show Gist options
  • Save cr0ybot/7efe671e87b2854e0165630b3fd5eb66 to your computer and use it in GitHub Desktop.
Save cr0ybot/7efe671e87b2854e0165630b3fd5eb66 to your computer and use it in GitHub Desktop.
Remove parent term selector for hierarchical taxonomies
/**
* Fool WordPress into thinking this taxonomy isn't hierarchical so it doesn't show the parent dropdown on the add/edit term page
*/
function disable_parent_category_dropdown() {
global $wp_taxonomies;
$wp_taxonomies['my_taxonomy']->hierarchical = false;
}
add_action( 'my_taxonomy_pre_add_form', 'disable_parent_category_dropdown' );
add_action( 'my_taxonomy_pre_edit_form', 'disable_parent_category_dropdown' );
/**
* Reverse the previous change after the add/edit term form
*/
function disable_parent_category_dropdown_reverse() {
global $wp_taxonomies;
$wp_taxonomies['my_taxonomy']->hierarchical = true;
}
add_action( 'my_taxonomy_add_form', 'disable_parent_category_dropdown_reverse' );
add_action( 'my_taxonomy_edit_form', 'disable_parent_category_dropdown_reverse' );
/**
* Remove the parent dropdown when adding a term from the edit post screen (non-gutenberg)
*/
function disable_post_parent_category_dropdown( $args ) {
if ( $args['taxonomy'] === 'my_taxonomy' ) {
$args['echo'] = false;
}
return $args;
}
add_filter( 'post_edit_category_parent_dropdown_args', 'disable_post_parent_category_dropdown' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment