WordPress: A URL redirection page template

Sometimes you want an archive page, a search page or a post to be accessible using a shorter or nicer URL.
Let’s say I want my author page to be accessible not only with the URL https://benohead.com/author/hb/ but also with the URL https://benohead.com/about-me/. You can of course just create this about-me page and put in some redirection code. Let’s say you want to do it more often and do not want to always have to remember the code to add to redirect. Then you’d need a special page template used for redirection and be able to configure the redirection URL in the individual pages.

There’s a perfect tool for this: the custom fields. You can just create a template reading a new custom fields (e.g. called “redirect_url”) which contains the URL you want to redirect to and redirecting the visitor to this URL.

You then just need to create a new page, choose this redirection template, set the custom field and publish the page.

Here’s a short description how it should be done.

First you have to create the redirection page template. A page template is very easy to create. Just create a new file in your theme directory e.g. wp-content/themes/mythemename/redirection-page.php. Make sure this new file has the same owners and access rights as the other files in the directory. This file is then visible in the Theme Editor. Open it there and add the following:

<?php
/**
 * Template Name: Redirection Template
 */
$redirect_url = get_post_meta($post->ID, 'redirect_url', true);
wp_redirect($redirect_url);
?>

Here a few explanations about the code:

/**
 * Template Name: Redirection Template
 */

This basically identifies the file as a page template which will be displayed as “Redirection”.

$redirect_url = get_post_meta($post->ID, 'redirect_url', true);

We fetch the custom variable using the get_post_meta function. The third parameter set to true tells WordPress that we do not want to get an array back but a single value.

wp_redirect($redirect_url);

This performs the redirection to the requested URL.

Another way to do it is:

<?php
/**
 * Template Name: Redirection Template
 */
$redirect_url = get_post_meta($post->ID, 'redirect_url', true);
header('Location: '.$redirect_url);
die();
?>

Now you can use this template when creating a new page:

template available

And you need to define the redirection URL in the custom field redirect_url. By default the custom field area is not displayed for pages. You need to enable it in the screen options:

screen options display custom fields

Then you will see the following below the page text:

empty custom fields area

If it is the first time you use this custom field, it will not be in the list and you’ll have to add it by clicking “Enter new”:

enter new redirect custom field

The value you set is the URL you want to redirect to. When done, just publish the page.

The only difference between the two solutions using wp_redirect and header is that with wp_redirect, you will see the redirection URL in the URL field in the browser and with header, you will see the URL of the redirecting page.

Leave a Reply

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