How to Create a Redirect Page Template in WordPress
19th Jun 2010 | Posted by Eko S. | 24 CommentsYou may have noticed a theme that has completely different looking pages, like archive page, contact page, redirect page, fullwidth-no sidebar page, homepage, etc. A custom page is helpful if you want to have a page that looks completely different than rest of your blog but still runs on the WordPress CMS.
In this tutorial we will show you how you can create a redirect page template in WordPress using the custom page template snippet.

Redirect page is a page for URL redirection, this page is very useful if you want to shorten your link address or it can also be used to hide your affiliate link.
This page template also we use in our premium wordpress theme : DTimes and Gudeg Theme
How To?
Step 1 Give the template name
You will begin creating a custom page by opening a blank file in your text editor and paste the following code at the very top:
<?php /* Template Name: Redirect */ ?>
Save the page as : redirect.php or you can name it any other structure you like as long as it is a php file.
The above code is simply giving your template a name that you will later see that WordPress recognize, so we are calling our template ‘redirect’.
Step 2 Redirect function
Now directly below, add this code
<?php if (have_posts()) : the_post(); ?>
<?php $URL = get_the_excerpt(); if (!preg_match('/^http:\/\//', $URL)) $URL = 'http://' . $URL; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Refresh" content="0; url=<?php echo $URL; ?&lgt;">
</head>
<body>
</body>
</html>
<?php endif; ?>
Here is an explanation for the code above :
-
<?php if (have_posts()) : the_post(); ?>
This code is to check whether or not there is content post.
-
<?php $URL = get_the_excerpt();
This code is to call the actual link address in redirect page.
-
if (!preg_match('/^http:\/\//', $URL)) $URL = 'http://' . $URL; ?>Then this code will check, if the address link already have ‘http://’? if not it will automatically be added.
-
<meta http-equiv="Refresh" content="0; url=<?php echo $URL; ?>">
This code as the waiting time to go to actual link address, if the greater its value it will be the longer time required.
Below the complete code for redirect page template:
<?php
/*
Template Name: Redirect
*/
?>
<?php if (have_posts()) : the_post(); ?>
<?php $URL = get_the_excerpt(); if (!preg_match('/^http:\/\//', $URL)) $URL = 'http://' . $URL; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Refresh" content="0; url=<?php echo $URL; ?&lgt;">
</head>
<body>
</body>
</html>
<?php endif; ?>
Step 3 Save it!
Once you have completed editing this file save it and upload it in your theme directory (/wp-content/themes/yourthemedirectory/) where files like single.php and index.php are located.
[updated (07/19/2010) : For the other way without have to worry about SEO, please check the comment below]
Thanks to kucrut.
Using The Redirect Page
- Now, login to your admin panel, write a new page. On the Page Template dropdown, select Redirect
- Add a title to the page (e.g. Free Themes)
- Add an URL to the content of the page (e.g. http://www.dynamicwp.net OR dynamicwp.net OR www.dynamicwp.net)
- Publish!
- Now the new link for http://www.dynamicwp.net (for example) will be http://yoursite.com/free-themes
Hope you liked this tutorial! Please let us know what you think, and make sure to either leave a comment below. We’d love to see it in action!
Thank you for this. I had a redirect template already but had no idea how to use it. Your instructions were PERFECT and easy to follow a. I'm so happy that I think this calls for a happy dance. Seriously, you made my day. Thank you!
please can somebody give a idea or a script how is gplus.to created i want to creat one for my country and i dont know how but i know somebody can help me please? Best Regards, please my email is aljapaco@gmail.com Please????
I see the example code in my comment dit not work very well... Here is the page with full info on an alternative: http://www.blogcatalog.com/discuss/entry/create-redirection-page-tamplate-for-wordpress
What about this: ID, 'redirect', true); wp_redirect($location); ?> Now create a custom field name "redirect" and in the value put the desired url and publish your page
Excellent publish! Finally somebody that knows the achievements about and may provide exellent articles to us visitors.Really looking forward to the following workout routine posting.Kenneth
Why not use the wp_redirect() function? This way you won't have to worry about SEO. You can just use the URL from the excerpt as the wp_redirect argument. No need to do anything past that :)
Hi, thanks for your input. Can you share with us more details how to use wp_redirect() function? Or give some resources? Regards
It's pretty simple, really. Just give the function a URL, and it will redirect you there. One thing to remember is, you need to call this function before the wp_head finishes loading or it'll give you a PHP header error. More here.
I stumbled on your blog by accident while searching in Google. Im glad I did because I ended up reading many of your posts. The information is very good. I may not agree with every post, but the information is very good. Keep up the good work.
@Lee - it uses a HTML meta refresh, so not SEO friendly. Would be better to use a PHP header() redirect. LIke the idea, but when you have many redirect pages (which we all do for all our affiliate links!), could potentially clog up the number of pages to administer in the wordpress admin page though.
Trackbacks




































[...] See the original post here: How to Create a Redirect Page Template in WordPress | DynamicWP [...]
[...] How to Create a Redirect Page Template in WordPress | DynamicWP Tags: template, tutorial, [...]
[...] How to Create a Redirect Page Template in WordPress | DynamicWP [...]
[...] This post was Twitted by bkmacdaddy [...]
[...] How to Create a Redirect Page Template in WordPress | DynamicWP [...]
[...] How to Create a Redirect Page Template in WordPress. In this tutorial we will show you how you can create a redirect page template in WordPress using [...]
[...] 8. How to Create a Redirect Page Template in WordPress. [...]
[...] How to Create a Redirect Page Template in WordPress [...]
[...] for your WordPress theme. View: How to create a built-in contact form for your WordPress theme 4. How to Create a Redirect Page Template in WordPressIn this tutorial we will show you how you can create a redirect page template in WordPress using the [...]
[...] (Code) [...]
[...] (Code) [...]