TSurF
TSurF

Reputation: 52

wordpress page php redirect

im struggling to find a way to redirect a wordpress page to a random predefined list of url's.

Each URL is a sub domain of the original domain, so:

page1.homepage.com, page2.homepage.com, page3.homepage.com

I know this code should appear before the doctype which i've done.

Now for some reason i cant get it to work! Any help would be much appreciated!

Here is my code:

    $urls           = array('http://1.home.com','http://2.home.com','http://3.home.com');
    $random_value   = array_rand($urls);
    $random_link    = $urls[$random_value];

    ("location: $random_link")

Upvotes: 1

Views: 58

Answers (1)

jlocker
jlocker

Reputation: 1488

In wordpress there is an built in function to do that wp_redirect,

use it like this,

$urls = array(
    'http://1.home.com',
    'http://2.home.com',
    'http://3.home.com'
);
$random_value = array_rand($urls);
$random_link = $urls[$random_value];
wp_redirect($random_link, 301);
exit;

Reffer for more information wp redirect

Upvotes: 1

Related Questions