How to Create a Redirect Page Template in WordPress

You 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 :

  1. <?php if (have_posts()) : the_post(); ?>

    This code is to check whether or not there is content post.

  2. <?php $URL = get_the_excerpt();

    This code is to call the actual link address in redirect page.

  3. 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.

  4. <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

  1. Now, login to your admin panel, write a new page. On the Page Template dropdown, select Redirect
  2. Add a title to the page (e.g. Free Themes)
  3. Add an URL to the content of the page (e.g. http://www.dynamicwp.net OR dynamicwp.net OR www.dynamicwp.net)
  4. Publish!
  5. 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! :)

15 Comments to “How to Create a Redirect Page Template in WordPress”

Add Comments (+)

  1. Lee says:

    Really like this idea but what impact will this have on search engines?

  2. @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.

  3. 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.

  4. Jennifer R says:

    It’s a simple trick :), thankyou but I think I can do it with Htaccess

  5. kucrut says:

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

    • Eko S. says:

      Hi, thanks for your input.
      Can you share with us more details how to use wp_redirect() function? Or give some resources?

      Regards

      • kucrut says:

        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.

  6. getsixpacks says:

    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

  7. Pixelbits says:

    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

  8. Pixelbits says:

    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

  9. metas says:

    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????

  10. Jenny Shih says:

    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!

  11. Fontanalorenz says:

    Hi! Here there’s a list of 9 WordPress template pages including this
    http://fontanalorenzo.it/wordpress/code/9-useful-wordpress-template-pages.html

  12. zhongxiaoge says:

    This is my WordPress Redirect page code as blew:
    <pre ><?php /**  * Template Name: Redirect Page  */  $url=$_GET["url"]; ?> <?php if ($url) : ?> <?php if (!preg_match(‘/^http:///’, $url)) $url = ‘http://’ . $url; ?> <?php wp_redirect($url); ?> <?php else : ?> <?php header(“HTTP/1.1 301 Moved Permanently”); header(“Location: “.get_bloginfo(‘url’).”#301-redirect-from-redirect-without-url-page”); ?> <?php endif; ?></pre>

Trackbacks/Pingbacks

  1. Blog Search » Blog Archive » How to Create a Redirect Page Template in WordPress | DynamicWP
  2. wp-popular.com » Blog Archive » How to Create a Redirect Page Template in WordPress | DynamicWP
  3. Super Squeeze Page Pack | Twitter Right
  4. Twitted by bkmacdaddy
  5. Epilepsy: always alarming: some seizures can be traced to an … « News, Articles and Information
  6. 35+ Fresh WordPress Tutorials and Resources of June 2010 | DynamicWP
  7. Wordpress Tutorials 2010 | SNilesh.com -:- Nilesh Shiragave
  8. Exclude Pages or Posts of Certain Categories from WordPress Search Result
  9. 20 Great Ways to add Bling to your Wordpress Blog
  10. 14 WordPress Page Templates To Make Life Easier
  11. 14 WordPress Page Templates To Make Life Easier | Wordpress Training
  12. 10+ WordPress page templates for all needs | CatsWhoCode.com
  13. 10 trang Templates cần thiết cho trang web Wordpress của bạn - Tổng hợp kiến thức vBulletin, Wordpress,lập trình PHP và SEO - LeNangHai.Net | Tổng hợp kiến thức vBulletin, Wordpress,lập trình PHP và SEO - LeNangHai.Net
  14. 10+ WordPress page templates for all needs Techbin

Leave a Reply

 

Amazingly Beautiful WordPress Themes