WordPress: Change the permalink of a post type afterwards

At Themeforest there are many cheap WordPress themes and these often have their own post types. Some post types are really great. For example, there are the occasional customer stories that you might want to rename so that the permalink is easier to read or simply in your language. WordPress offers a hook for this and with this hook we can change the arguments that were defined when we created the post type.

The hook or the filter for the change is register_post_type_args and we can control this at any time. If we now have a post type Customer Stories (customer_story) and want to rename the slug for the URL to customers, we have to do the following:

<?php // functions.php

/**
 * Filters all content types and changes the permalink of customer stories.
 *
 * @param array $args The original arguments of the content type.
 * @param string $post_type The post type.
 *
 * @return array
 */
function pixelbart_modify_customer_stories( $args, $post_type ) {
    if ( 'customer_story' !== $post_type ) {
        return $args;
    }

    $args['rewrite']['slug'] = 'customers';

    return $args;
}

/**
 * Here the new arguments are transferred to the customer stories.
 */
add_filter( 'register_post_type_args', 'pixelbart_modify_customer_stories', 10, 2 );

First we check, within the pixelbart_modify_customer_stories function, if it is the post type we want to change. If so, we tell the post type that the slug for the permalinks is customers and no longer customer_story. We put all this in the functions.php of our WordPress theme or in our own WordPress plugin.

To make WordPress recognize the changes, we have to open the WordPress Admin under Settings and select Permalinks. Then all permalinks within WordPress will be checked and loaded automatically. Tip: If you have deleted your .htaccess, you can create it there again. WordPress takes care of the rest and you have no more headaches.

Important is that you can change all arguments of a post type with register_post_type_args. So if a description doesn't really fit, you can also change the labels afterwards.

Result

Your permalink should then no longer be domain.com/customer_story/ but domain.com/customers/. Your customer stories are now available and it's better for your search engine optimization because you have translated everything into your language. Now you definitely know how to retouch your content types to make them look better.

No Comments Yet