Results 1 to 2 of 2

Thread: Using the wp_redirect() function

  1. #1
    Founder dcowgill's Avatar
    Join Date
    Mar 2009
    Location
    San Francisco, CA
    Posts
    1,939
    Thanks
    66
    Thanked 135 Times in 99 Posts

    Using the wp_redirect() function

    Did you know that WordPress provides a function which allows you a safe way to redirect to any URL?

    It's a pluggable function called wp_redirect() which takes two parameters ($location and $status).

    So what's a good use case? Say you've got an old page template (old-page) and you want to redirect visitors to the new page. Not only that, you want it to be a permanent redirect which means Google transfers your page rank & inbound link juice to the new page.

    Here's how you would do it.

    Edit your page template and at the top, paste in the following code:

    PHP Code:
    <?php

    wp_redirect
    'http://www.mysite.com/new-page/'301 );
    exit;

    ?>
    The first parameter is the new url you want to redirect visitors to. The second parameter is the type of redirect. 301 means "permanently moved" which is important to tell search engines. (see wikipedia for all http status codes)

    Just make sure that the function is called before any output is sent to the browser otherwise you'll likely get a "headers already sent" error.

    Without this function, the old method would be to use the following PHP code instead:

    PHP Code:
    <?php

    header
    ("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.mysite.com/new-page/");

    ?>
    That's it. Now you can easily use the safer WordPress redirect function instead of the old php header method.

    For full function reference, see the wp_redirect() page in the WordPress Codex.

  2. #2
    Marketplace Seller mr_green's Avatar
    Join Date
    Aug 2010
    Posts
    953
    Thanks
    113
    Thanked 192 Times in 147 Posts
    Useful...
    Thx

    New part of the forum

    But
    all of a sudden I'm afraid.User of forum have no association with the Taliban or hippy
    Best

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Usability function for 3.0.5.2
    By frcamp in forum Report ClassiPress Bugs
    Replies: 4
    Last Post: December 2nd, 2010, 06:30 AM
  2. function cp_ad_region_widget()
    By savvar in forum Report ClassiPress Bugs
    Replies: 1
    Last Post: October 3rd, 2010, 10:09 AM
  3. Where is the search function ?
    By yoodac in forum Report ClassiPress Bugs
    Replies: 2
    Last Post: August 26th, 2010, 03:57 PM
  4. Can't Call New Function File
    By scarstens in forum Help Using ClassiPress
    Replies: 0
    Last Post: April 27th, 2010, 05:10 AM