Created
March 18, 2021 18:46
-
-
Save cr0ybot/7efe671e87b2854e0165630b3fd5eb66 to your computer and use it in GitHub Desktop.
Remove parent term selector for hierarchical taxonomies
This file contains 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
/** | |
* 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