Change the URL Slug of Any Taxonomy
Changes the URL base of a category-style taxonomy registered by a theme or plugin, for example from /portfolio-category/ to /work-type/.
Opens WPIDE on
Code
// Taxonomy => new slug.
$wpide_new_tax_slugs = array(
'portfolio_category' => 'work-type',
);
add_filter( 'register_taxonomy_args', function ( $args, $taxonomy ) use ( $wpide_new_tax_slugs ) {
if ( isset( $wpide_new_tax_slugs[ $taxonomy ] ) ) {
$args['rewrite'] = array_merge( is_array( $args['rewrite'] ?? null ) ? $args['rewrite'] : array(), array( 'slug' => $wpide_new_tax_slugs[ $taxonomy ] ) );
}
return $args;
}, 10, 2 );
Notes
Visit Settings > Permalinks once afterwards. For the built-in category and tag bases, use Settings > Permalinks instead.
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.