Simple PHP Redirect in WordPress

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">
Posted in ,

13 thoughts on “Simple PHP Redirect in WordPress”

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

      1. Hi Alex

        I am wanting to redirect multiple pages 3-5 in total what is the exact code structure I need to use?. I tried the if/elseif/ type structure but could not get it to work….for the single page name it is brilliant!.

        Regards
        Kevin

        1. Hi Kevin! I just tested it using multiple if statements, and it worked for me. If that’s not working for you, feel free to use my contact form to send me a copy of your code and I’ll try to take a look. You might also want to look into the Page Links To plugin, depending on your use case.

  2. Hey Alex, It’s possible to do delay redirect with PHP. Something like this will work.

    header( “refresh:5;url=wherever.php” );

    Correct me if I am wrong.

    1. Thanks for the comment Dhruv! You are absolutely correct. I’ve updated the post to include your contribution.

  3. I am looking for a way to make it so if someone/something goes to /sitemap.xml that it will send them to /sitemap/ and would like to make it a part of the theme

    So you know of a way to do that?

    1. If you’re trying to create the redirect from within WordPress, you might want to give the “Redirection” plugin a try. I haven’t tested it with a sitemap.xml file though, so I can’t make any promises.

      You should also be able to create the redirect using your .htaccess file.

  4. works fine, i will do something like this then:

    function pixelpro_redirect() {
    if(is_page(‘sample-page’)){
    $url = ‘http://myurl.com/landingpage’;
    wp_redirect( $url );
    exit;
    }
    }
    add_action(‘wp_head’, ‘pixelpro_redirect’);

Leave a Comment

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