Simple PHP Redirect in Wordpress

Note: This post was originally written for my blog over at sackclothstudios.com. However, with the changes I’m making to that site, I figured the post would be of more use here on this site.

Sometimes when using Wordpress you want to include a non-Wordpress page in your main navigation while still maintaining the simplicity of the wp_list_pages() function. I know there are plugins that do this type of thing, but many of them are just overkill. I just needed one non-Wordpress page on my menu, so here’s what I did. First, I created a page from within Wordpress and gave it a heading but left the body blank. My page was called “Purchase” but you can name it whatever you’d like. This is the name that will show up in your navigation menu. Then I went to the header.php file of my theme and added this at the top:

<?php
if(is_page('purchase')){
header("HTTP/1.1 301 Moved Permanently");
header("Status: 301 Moved Permanently");
header("Location: http://www.example.com/");
header("Connection: close");
exit(0); // Optional, prevents any accidental output
}
?>

Obiously, you’ll want to change the if statement to check for the blank page that you set up. Then change “http://www.example.com/” to whatever page you want the redirect to point to. Make sure that you put the code at the very top of the header.php file. If something else comes before it, it won’t work.

I first found this PHP redirect on  Steven Hargrove’s blog.  He gives examples of this in a number of different languages. However, I adapted it to Wordpress and added the ” header( “Connection: close”);” line to get it working properly. I’d love to hear how you solve this little Wordpress problem. If you have a different solution, please leave a comment below with a brief explaination. Thanks.

Update: If you want to make a temporary redirect (302), rather than a permanent one (301) use the code below:

<?php
if(is_page('purchase')){
header("Location: http://www.example.com/");
header("Connection: close");
exit(0); // Optional, prevents any accidental output
}
?>
Algen asked if it was possible to do a delayed redirect. It is possible, but not with PHP. Here is the HTML necessary to redirect a page after 30 seconds:
<meta http-equiv="refresh" content="10;url=http://www.example.php">

Category: Hacks, Wordpress Follow comments with RSS 2.0

2 Responses to “Simple PHP Redirect in Wordpress”

  1. Karl | August 29, 2010 at 6:19 am

    Hi, Nice tip, works a treat, but how would you go about doing it on multiple pages, would you just keep repeating the steps, or can you creat if else steps, saving having all the code again in the header.

    Cheers
    Karl

  2. alexmansfield | September 1, 2010 at 2:19 pm

    You could either repeat the steps or use an if/elseif/ type structure. Just use whichever one is most comfortable to you.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>