Mangcoding

icon chat
Mulida Asti - Friday, 7 March 2025 - 9 months ago

How to Change Post URL in WordPress via functions.php

single image
Photo by John Schnobrich On Unsplash

If you’re reading this article, you likely have some knowledge about the WP_Rewrite class. This class allows you to modify WordPress permalinks, rewrite rules, and change their structure. Keep reading, because here you’ll learn an alternative method on How to Change Post URLs in WordPress via functions.php without using WP_Rewrite.

In this article, we won’t rely on WP_Rewrite. Instead, I’ll show you a manual method to change permalinks for specific objects on your blog (whether it’s post types or taxonomies). This method involves three simple steps: redirecting, modifying the query request, and rewriting the link.

Link Mangcoding

Step 1. Redirect with «template_redirect»

To ensure that your old URLs still work, you should set up a 301 redirect from the old URLs to the new ones (you can also handle this via .htaccess).

Note: In the example below, I’m using category as the default category prefix and tag as the default tag prefix.

Place the following code into your current theme’s 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();
        }
    }
}


add_action('template_redirect', 'rudr_url_redirects');

As you can see, this code efficiently handles URL processing and redirection.

Link Mangcoding

Step 2. Modify the Request with the «request» Hook

The next step is to tell WordPress how to handle the rewritten URLs. Here’s how to set it up:

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);

Link Mangcoding

Step 3. Rewrite the Links

Mengubah URL Postingan pada WordPress

If you don’t want the browser to display the old URLs when hovering over links, follow this method.

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);
Custom Permalinks Plugin as an Alternative

If you do not want to modify or add code to the functions.php file, I recommend using a plugin such as Custom Permalinks.

Example for categories:

Cara Mengubah URL pada WordPress dengan Plugin

That’s the end of the guide on How to Change Post URLs in WordPress via functions.php. Hopefully, this tutorial helps you manage your WordPress permalinks more effectively. Feel free to try out the steps described above, and if you have any constructive feedback or questions, please drop a comment or contact Mangcoding through email or social media.

Thank you!

Reference: How to Change Post URLs in WordPress via functions.php

Link Copied to Clipboard