26

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/?

  • I just solved this same issue by hooking into init with a priority of 20 and then calling register_post_type again for the same post type. Anyone know if this is going to cause me any trouble down the road? – Dustin Apr 12 '13 at 2:48
21

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:

function movies_register_post_type() {

    register_post_type(
        'movies',
        array(
            'labels' => array(
                'name' => __('Movies'),
                'singular_name' => __('Movie')
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'movies'
            )
        )
    );

} // end example_register_post_type
add_action('init', 'movies_register_post_type');

You can modify the options table by providing your own custom rules based on the existing post type.

Basically, we'll do this:

  • Take the existing set of rules and then write our own with our own custom slugs
  • Give the new rule a higher priority than the custom post type's slug

Here's how you can do this:

function add_custom_rewrite_rule() {

    // First, try to load up the rewrite rules. We do this just in case
    // the default permalink structure is being used.
    if( ($current_rules = get_option('rewrite_rules')) ) {

        // Next, iterate through each custom rule adding a new rule
        // that replaces 'movies' with 'films' and give it a higher
        // priority than the existing rule.
        foreach($current_rules as $key => $val) {
            if(strpos($key, 'movies') !== false) {
                add_rewrite_rule(str_ireplace('movies', 'films', $key), $val, 'top');   
            } // end if
        } // end foreach

    } // end if/else

    // ...and we flush the rules
    flush_rewrite_rules();

} // end add_custom_rewrite_rule
add_action('init', 'add_custom_rewrite_rule');

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 the init 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.

  • Worked beautifully! Could you also point me in the right direction for updating the permalinks? – Edbury Feb 13 '12 at 20:23
  • 1
    Depending on your theme, you can do this at the template level. When you see the call to 'the_title()', you can replace that with $title = get_the_title(), then string replace '/movies/' in $title with '/films/'. – Tom Feb 14 '12 at 13:16
  • What if the slug you want to change to is dynamically created by a meta value of the current post? I need to be able to use $post in the add_custom_rewrite_rule() function but it's throwing errors in WP Admin saying 'Trying to get property of non-object'. How can I get $post to get the meta value? – Lee Jan 17 '18 at 16:15
4

This code worked well for my child theme. Needed to change "program" slug to "place".

/*
CHANGE SLUGS OF CUSTOM POST TYPES
*/

function change_post_types_slug( $args, $post_type ) {

   /*item post type slug*/   
   if ( 'program' === $post_type ) {
      $args['rewrite']['slug'] = 'place';
   }

   return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );

/*
CHANGE SLUGS OF TAXONOMIES, slugs used for archive pages
*/

function change_taxonomies_slug( $args, $taxonomy ) {

   /*item category*/
   if ( 'program-category' === $taxonomy ) {
      $args['rewrite']['slug'] = 'locations';
   }

   return $args;
}
add_filter( 'register_taxonomy_args', 'change_taxonomies_slug', 10, 2 );
  • It works perfectly, I have also used it for the tags adding the relative rule, it is necessary to update the permalinks by executing a save from the settings page > permalink. Thank you! – Lorenzo Magon Oct 19 '18 at 18:17
  • Works perfectly to change other args too, I needed to set $args['has_archive'] = false and this worked. – Mere Development Feb 8 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.