In this lesson we will teach you two ways to redirect links in WordPress Without any plugin.
In the first method, we hardcode the source and destination links into our code, This method is easier to set up and is suitable for small number of links.
In the second method, which requires more setups, we perform dynamic redirection using custom fields. If you have many links for redirection, this method will be ideal for you.

Step 1: Get the From(Old) and To(New) URL Slug

What is slug? The “slug” is that part of the URL without the domain name.
Imagine the URL is “https://yoursite.com/product-category/blue-shirt/” , the slug would be “/product-category/blue-shirt/”.

url slug in wordpress

In this example we will redirect “https://redpishi.com.com/old-product-category/old-blue-shirt/” to “https://redpishi.com.com/new-blue-shirt/“. The slugs will be as follows:

from: /old-product-category/old-blue-shirt/
to: /new-blue-shirt/

First method: (Hardcoded URL)

Step 2: Add custom code in functions.php

In the code below change the [from slug] and [to slug] to your “from” and “to” slugs, then copy and save this code in your child’s theme functions.php.
If you do not know how to make a child theme, read the following article.
Create child theme in WordPress step by step [without plugin]

function redirect_page() {

     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }

    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);

    switch ($currenturl_relative) {
    
		case '[from slug]':
			$urlto = home_url('[to slug]' );
			break;

        default:
            return;
    
    }
    
    if ($currenturl != $urlto)
        exit( wp_redirect( $urlto ) );


}
add_action( 'template_redirect', 'redirect_page' );

That’s it.
If you have more links to redirect, copy the following portion of above code and paste the rest of the links in it.

case '[from slug2]':
	$urlto = home_url('[to slug2]' );
	break;

Second method: (Build a simple dashboard with the help of custom fields)

Step 2: Add custom code in functions.php

Delete the previous code and copy and save this code in your child’s theme functions.php.


/* redirect by redpishi.com */ 
function redpishi_com_redirect() {
	$redirectArr = array();
	$id = get_page_by_title( 'redirect' )->ID;
	$rowArr = get_post_meta($id);	
	foreach ($rowArr as $key => $value) {	
	 if (strpos($key, "/") === 0) {
    $redirectArr[trim($key)] = trim($value[0]);	}			
	}
     if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
        $protocol = 'https://';
        }
        else {
        $protocol = 'http://';
    }
    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);
	if (substr($currenturl_relative, -1) == '/') { $currenturl_relative = substr_replace($currenturl_relative ,"",-1);}
	$forceredirect = 0;
	foreach($redirectArr as $from => $to) {
		if (substr($from, -1) == '/') { $from = substr_replace($from ,"",-1);}
		if( $currenturl_relative == $from) {
			$forceredirect = 1;
			$urlto = home_url($to);
		}}

    if ($forceredirect == 1) {
		 wp_redirect( $urlto);
 		   exit;		
	}
}
add_action( 'template_redirect', 'redpishi_com_redirect' );

Step 3: Build a page and activate custom fields

Create a new page in WordPress and name it redirect. (Important: page name must be redirect).
Check if Custom fields is active on the screen, if not enable it. Go to the page settings (3 dots at the top right of the screen) and select Preferences from the menu, Then select the Panels tab and make sure the Custom fields option is on.

activate Custom fields in WordPress

Next, change Visibility to Private (for security Reasons) and publish the page.

Step 4: Enter and save your links in Custom fields

In the last step, create and save a Custom field for each link. Note that we still use slugs (step 1). In the name field put “from slug” and in the Value field put “to slug” and click on Update.

Redirect Pages In WordPress by Custom fields

You can add as many Custom fields as you like.
Congratulations, you successfully created a redirect link management panel.

If this article is difficult for you to read in text, you can watch the video version below.


Have you started redirecting pages on your site? Which method do you prefer? Let us know in the comments below.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 50

Leave a Reply

Your email address will not be published. Required fields are marked *