Update: there is also a WordPress function for redirects: wp_redirect()
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:
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
}
Obviously, 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 #1: If you want to make a temporary redirect (302), rather than a permanent one (301) use the code below:
if(is_page('purchase')){
header("Location: http://www.example.com/");
header("Connection: close");
exit(0); // Optional, prevents any accidental output
}
Update #2: Algen asked if it was possible to create a delayed redirect. It is possible, using either PHP or HTML. As Dhruv pointed out in the comments, PHP handles it like this:
header( 'refresh:5;url=http://www.example.com' );
If you’d prefer to use HTML, it can be done like this:
<meta http-equiv="refresh" content="10;url=http://www.example.com">