Change the URL Slug of Any Post Type
Changes the URL base of a post type registered by a theme or plugin, for example from /portfolio/ to /work/, including its archive.
Opens WPIDE on
Code
// Post type => new slug.
$wpide_new_slugs = array(
'portfolio' => 'work',
);
add_filter( 'register_post_type_args', function ( $args, $post_type ) use ( $wpide_new_slugs ) {
if ( ! isset( $wpide_new_slugs[ $post_type ] ) ) {
return $args;
}
$args['rewrite'] = array_merge( is_array( $args['rewrite'] ?? null ) ? $args['rewrite'] : array(), array( 'slug' => $wpide_new_slugs[ $post_type ] ) );
if ( ! empty( $args['has_archive'] ) ) {
$args['has_archive'] = $wpide_new_slugs[ $post_type ];
}
return $args;
}, 10, 2 );
Notes
Visit Settings > Permalinks once afterwards, and redirect the old URLs if the site is already indexed.
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.