Is it possible to redeclare/change the slug of a plugin’s existing custom post type (without simply editing the plugin)?
That is, if Plugin X creates a custom post type with the slug /uncookedtoast/, is it possible to add a filter to functions.php (or something similar) that changes the slug to /bread/?
Yes, this is possible, but if the plugin is creating a custom post type using the
rewrite => array('slug' => 'post_type')
parameter, then it’s not likely that you’re going to be able to replace the slug.Whenever custom post types are created, URL rewrite rules are written to the database. Depending on which action triggers the creation of the custom post type (such as the init action), WordPress will flush the rewrite rules and restore the custom post type’s slugs regardless of what changes you attempt to make.
That said, you can provide custom slugs for the custom post types. The following example assumes that you have a custom post type of
movies
and that you’re attempting to change the/movies/
slug to/films/
.To be complete, here’s the basic function used to define the
movies
custom post type. The plugin that you’re referencing should be doing something like this:You can modify the options table by providing your own custom rules based on the existing post type.
Basically, we’ll do this:
Here’s how you can do this:
Now, you’ll have two ways to access your movies:
/movies/Back-To-The-Future
/films/Back-To-The-Future
Note that I don’t recommend hooking the
add_custom_rewrite_rule
into theinit
action as it will fire too frequently. This is just an example. A better place to apply the function would be on theme activation, plugin activate, perhaps the save_post action, etc. Depending on what you need to do, you may only need to fire it once or just a few times.At this point, you may want to consider updating the permalinks for the custom post type to use the ‘
/movies/
slug. For example, if you navigate to/films/
, you will see a listing of all your movies but hovering over the title will reveal that the/movies/
slug is still being used.To go one step further, you could technically instate a 301 redirect to catch all links to
/movies/
to redirect to their/films/
counterpart, but this all depends on what you’re attempting to do.This code worked well for my child theme. Needed to change “program” slug to “place”.
In my case there was a plugin (needed for the theme to work) registering a custom post type and I wanted to change some things on it, the slug among others.
So, what I’ve done is to re-register (kind of overwrite of the plugin’s registering function) the custom post type by adding the following in my child theme’s
functions.php
file like so:As you can see, you can change the slug by simply adding the following to the
$args
array:More infos on this at: https://developer.wordpress.org/reference/functions/register_post_type/
VERY IMPORTANT
Remember to refresh you permalinks in order the changes to be applied and so you can access the involved posts. Go to the permalinks settings and simply press “Save”..then it should work!ð
In my case, plugin declares custom post type “portfolio” with
'rewrite' => array( 'slug' => 'portfolio')
, with a hook in the ‘init’ actionadd_action( 'init', 'some_plugin_custom_posts_init' );
.The above snippet, placed in a theme’s functions.php file, defines a new function
change_portfolio_slug()
that modifies the rewrite argument for the portfolio post type, changing its slug to “project”. We then use theregister_post_type()
function to register the post type with the modified arguments.The
add_action()
function hooks thechange_portfolio_slug()
function to the init action, so that it is executed when WordPress initializes.In the last line of the snippet
add_action( 'init', 'change_portfolio_slug', 10 );
, the number10
is the priority of the action hook. If you want to change the priority, you could change the10
to a different number. In my case, the working priority was20
.Note that you may need to refresh your permalinks after making this change to ensure that the new URL structure is applied correctly. You can do this by going to Settings > Permalinks in your WordPress admin dashboard and clicking the “Save Changes” button.