WordPress: Change the permalink of a post type afterwards

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
Search for a command to run...

Speaks many languages, but currently only uses PHP, JavaScript and WordPress (I consider it its own language, since it's a big sandbox).
No comments yet. Be the first to comment.
Recently, a friend asked me to send a script to remove pre tags from a string, including content. He uses a script to calculate the reading time for the content, but doesn't want the pre-tags to be included in the scoring. So if you ever have a simil...

When there are problems with one's WordPress website, it happens that one has to create a user without having access to the database or the WordPress admin. Then it is useful to be able to create or log in a user using FTP. So here you will learn how...

Occasionally it happens that overlaps occur in a large table and the message Duplicate entry '0' for key 'PRIMARY' for query then gets the upper hand in the error logs. This error can be fixed quite easily with a short line of SQL. Fix duplicate entr...

WordPress is packed with useful features. It is easy to lose the overview. Here you will find a selection of functions that deal with arrays and objects. A small collection of useful functions that you can always use, if you know that they exist. wp_...

As a rule, searches are created with the MySQL LIKE. This usually works very well, as long as the database table is not too large. However, if you work with a large database (millions+ entries), then you quickly notice how long a search in it actuall...

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