WP_Redirect()

WordPress has a handy function, wp_redirect(), for sending a user to a different page. It’s an easier way to handle redirects than to invoce the header() function on your own.

To do a 301 redirect, it’s as simple as this:

wp_redirect('http://www.example.com/', 301);

Just make sure that the function is called before any output is sent to the browser, otherwise you will get a “headers already sent” error.

It’s a bit easier than the normal way to go about redirecting:

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.example.com/");

The 301-type redirect means that the URL that the request is being forwarded to is the “real” URL, and that the client should recognize that. A 302, the default for both methods, is less preferable, designating a “temporary” redirect. Generally you should use a 301.

  • http://omninoggin.com Thaya Kareeson

    I will have to add this function into my plugins. Thanks Matt.

  • http://www.codingfriends.com genux

    Which plugin can you add this to before the headers are sent out ?