Default Term for a Custom Taxonomy
Gives a custom taxonomy a default term, like "Uncategorized" for categories, so items saved without a term still get one.
Opens WPIDE on
Code
// Taxonomy => default term name and slug.
$wpide_default_terms = array(
'genre' => array( 'name' => 'General', 'slug' => 'general' ),
);
add_filter( 'register_taxonomy_args', function ( $args, $taxonomy ) use ( $wpide_default_terms ) {
if ( isset( $wpide_default_terms[ $taxonomy ] ) && empty( $args['default_term'] ) ) {
$args['default_term'] = $wpide_default_terms[ $taxonomy ];
}
return $args;
}, 10, 2 );
Notes
WordPress creates the term on the next page load if it does not exist. Works for taxonomies registered on init by any plugin or theme.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.