How to Change URL of a custom post / page / category / tag in theme functions.php. Capitalize URL.
If you read this post, you should know something about WP_Rewrite class. It allows you to change WordPress permalinks rewrite reules, change the structure of them.
In this post I won’t use WP_Rewrite, I just want to show you another way of changing permalinks of a certain object on your blog (any post type or any taxonomy). It is the manual way and consists of 3 simple steps: redirect, change query request, link rewrite.
Step 1. Redirect using «template_redirect»
If you want your pages to be accessible by the old URLs, set redirect 301 from all old URLs to the new ones (it can be implemented via .htaccess
as well).
Note: In all examples below I use category
as default categories prefix and tag
as default post tags prefix.
Insert the following code to your current theme functions.php
.
function rudr_url_redirects() {
/* in this array: old URLs=>new URLs */
$redirect_rules = array(
array('old'=>'/category/uncategorized/','new'=>'/category/Uncategorized/'), // category
array('old'=>'/contacts/','new'=>'/Contacts/'), // page
array('old'=>'/hello-world/','new'=>'/hello-planet/'), // post
array('old'=>'/tag/wordpress/','new'=>'/tag/WordPress/') // post tag
);
foreach( $redirect_rules as $rule ) :
// if URL of request matches with the one from the array, then redirect
if( urldecode($_SERVER['REQUEST_URI']) == $rule['old'] ) :
wp_redirect( site_url( $rule['new'] ), 301 );
exit();
endif;
endforeach;
}
add_action('template_redirect', 'rudr_url_redirects');
You may notice that I use this code mostly to capitalize URLs.
Step 2. Change request using hook «request»
Great, the redirects are ready. Now we need to make WordPress understand if the URL was rewritten.
function rudr_rewrite_request($query){
$request_uri = urldecode($_SERVER['REQUEST_URI']);
/* for categories */
if( $request_uri == '/category/Uncategorized/' )
$query['category_name'] = 'uncategorized';
/* for pages */
if( $request_uri == '/Contacts/' ){
$query['pagename'] = urlencode('contacts');
unset($query['name']);
}
/* for posts */
if( $request_uri == '/hello-planet/' )
$query['name'] = 'hello-world';
/* for tags */
if( $request_uri == '/tag/WordPress/' )
$query['tag'] = 'wordpress';
return $query;
}
add_filter( 'request', 'rudr_rewrite_request', 9999, 1 );
Step 3. Rewrite the links
If you do not want your browser to show old URL when you hover on a link, use the code below.
For posts and pages
function rudr_post_permalink( $url, $post ){
if( !is_object( $post ) )
$post = get_post( $post_id );
$replace = $post->post_name;
/* We should use a post ID to make a replacement. It is required if you use urf-8 characters in your URLs */
if( $post->ID == 1 )
$replace = 'hello-planet';
if( $post->ID == 12 )
$replace = 'Contacts';
$url = str_replace($post->post_name, $replace, $url );
return $url;
}
add_filter( 'post_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'page_link', 'rudr_post_permalink', 'edit_files', 2 );
add_filter( 'post_type_link', 'rudr_post_permalink', 'edit_files', 2 );
If you do not know where to get a post ID, or a term ID, leave your comment and I will describe it for you.
For categories and tags
function rudr_term_permalink( $url, $term, $taxonomy ){
$replace = $term->slug;
/* by ID as well */
if( $term->term_id == 5 )
$replace = 'Uncategorized';
if( $term->term_id == 55 )
$replace = 'WordPress';
$url = str_replace($term->slug, $replace, $url );
return $url;
}
add_filter( 'term_link', 'rudr_term_permalink', 10, 3 );
Custom Permalinks plugin as an alternative
If you do not want to work with the code in your functions.php
, I recommend you Custom Permalinks plugin then.
Example for categories:

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Great explanation!!
Would it be possible to create a rewrite rule for a custom post type and categories?
Something like:
CPT = Cars
Category = BMW
Post Name = Sedan
Resulting URL = www.mysite.com/cars/bmw/sedan
Also, would it be possible for a post with multiple categories to generate multiple URL’s? Like, if I have a bmw and mercedes sedan
CPT = Cars
Categories = BWM, Mercedes
Post Name = sedan
Resulting URL = www.mysite.com/cars/bmw/sedan
Resulting URL = www.mysite.com/cars/mercedes/sedan
Thanks!!
Please, how can i make
$_SERVER["REQUEST_URI"]
to point to a custom file and not the index.php?Hi Phil,
Not sure, but maybe it will help:
$_SERVER["REQUEST_URI"] = 'custom url';
Please can you help me?
If I remove remove the category base /category/ via Yoast SEO I end up with a 404 error.
Hi Romain,
I recommend you to do it with this tutorial. You could also try to go to Settings – Permalinks and just update the settings without changes – sometimes it helps.
Ok thank you I’ll make a try
Another question about your code :)
How can I make it work if I want to change all my categories, taxonomies, tags, posts, pages?
In fact what I really need, is to push a variable, before every permalink on my website, like a translation system
http://www.mywebsite.com/en/category/test,
http://www.mywebsite.com/fr/category/test,
http://www.mywebsite.com/de/category/test…
I have no ready code for your task, sorry :)
Hi sir i want to change a custom post type category link tour-destination to flight-destination is there a way please tell
Hi,
Yes, you have to find
register_taxonomy()
function in your website code and make a replacement there.Hello Misha, awesome article!
Hopefully this will help to change my permalinks on single posts.
I’ve been searching for days to find a good answer, but I just can’t seem to find a good solution.
I was wondering, is it possible to use $attachment_title and set that data as a permalink on a single post?
I hope you can guide me in the right direction.
Hello Kevin,
What is $attachment_title ?
The title of an image attachment in WordPress. But you can remove my question, since I’ve hired a developer to do the job for me. Cheers
Ok, good luck!
Hey Alexander,
To be honest I’m not so good at using
add_rewrite_rule()
function, and my tutorial is about another method.Hey Misha, That’s really not a problem as i have solve he query and if it is possible i want to write a tutorial on this for your blog that will help the audience to understand easily. Cheers :)
Hey 🙃
Ok, you can send me your draft by email. If I like it, I will publish it with your authorship and all your links inside.
Let me know your email so i will send you draft as soon as possible.
You can do it on this page.
I had set up a taxonomy “authors” using yoast simple taxonomies and now i moved all the data to pods taxonomy ‘author”. The old “authors” is empty.
How could i redirect the old url “authors/” to the new “author/”. Thanks in advance. Any help will be awesome.
You can do it in your
.htaccess
file with this line:Please do not forget to replace rudrastyh.com with your domain.
Thanks a ton for the reply and the solution. It worked like a charm. I have been pecking on this for the past two days. You are just awesome!
Hello,
I need help in creating a custom url.There is one page About-us.I create a post listing under this page.every post has one gravity form. I want to redirect url on the submit.Recently my url has been page_name/post_name and I want to redirect on Thank-you page.So I want page_name/post_name/Thank-you. (Than-you can be anything like page/post/tag/category)
Thanks in advance.
Can I rewrite from: domain.com/?add-to-cart=123
to: domain.com/add-to-cart/name-of-the-product
How is it possible to do? Thanks in advance!!!