Skip to content
Advertisement

WordPress redirect based on the presence of a cookie

First off, I am a complete amateur when it comes to coding, but I’ve been reading hundreds of posts on Stack Overflow and other websites in an attempt to string together the right PHP code to make my website behave in a certain way. It’s probably going to look terrible and be formatted wrong, but I hope the idea of what I’m trying to accomplish comes through.

Before we get into the code, the basic idea is this: when someone comes to my e-commerce website I’d like to present them with three (or more) “brands”. When they click on their favorite brand and go the brand page, the website should drop a cookie in their browser. When they return to the website at a later date, I’d like the website to automatically redirect to their chosen brand page rather than presenting the three choices again.

The simplified code I’ve created as a proof of concept uses two brand options, where two cookies with the same name but different values are placed based on when the visitor goes to the particular brand page. Only the second cookie value (brand_2) causes the redirect in this simplified code. The first cookie, (value “brand_1”) drops in automatically when the visitor comes to the site. I don’t love that solution or even know if I need to do that, but it seems to work to a degree. When the visitor goes to the brand 2 page, the cookie value changes to “brand_2” as it should. When they go to the brand 1 page, the cookie value reverts back to Brand_1 as it was when they first arrived on site.

Here is the code for the cookies, not perfect but it appears to work:

function brand_cookies() {
    if(!isset($_COOKIE['brands'])){
        setcookie('brands', 'brand_1', time()+3600*24*365*3, 
SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
    }elseif(is_page('brand1') || is_page('brand2')){
        if($_COOKIE['brands'] == 'brand_2'){
            setcookie('brands', 'brand_1', time()+3600*24*365*3, 
SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
    }elseif($_COOKIE['brands'] == 'brand_1'){
        setcookie('brands', 'brand_2', time()+3600*24*365*3, 
SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
        }   
    }   
}
add_action( 'wp', ‘brand_cookies',10,1);

The part that doesn’t work is my wp_redirect, which causes the white screen of death when I add it below the above code in my functions.php file in wordpress. It does work to a degree though, as when the cookie “brand_2” is present, the website does redirect to the brand 2 page of the site. It’s just all white.

Here is the code for the redirect:

add_action( 'template_redirect', ‘brand_redirect’ );

function brand_redirect(){
    // do your check and call wp_redirect here
    if($pagename !='home' ) {
        if($_COOKIE['brands'] == 'brand_2' ) 
            wp_redirect( 'http://test.site.com/brand2' ) ;
            exit; }

}

My ultimate goal is to add more cookies to the upper code for my three or more brands and their associated pages, and then create the corresponding redirect in the code below. Hopefully this all makes sense! Any help you could offer on what I’m doing wrong that is causing the white screen of death would be appreciated! Thank you for your help!

UPDATE:

Using Will Hine’s suggestion for the top script worked absolutely perfectly for getting cookies added to the browser based on when the visitor to the site reached a particular brand page. Huge thanks to him for helping me with this! Here’s his script:

function brand_cookies() {

    $brands = [
       'brand1' => 'brand_1',
       'brand2' => 'brand_2',
       'brand3' => 'brand_3'
    ];

    foreach ($brands as $page => $cookie) {
        if (is_page($page)) {
        setcookie('brands', $cookie, time()+3600*24*365*3, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
        }
    }
}

I thought I was starting to figure out redirects and attempted to write my own script based on Will’s script above to trigger a re-direct from the home page to the appropriate brand page. I only set it to redirect based on one of the brand cookies rather than all three just as a test. Unfortunately, when I uploaded the script into my functions.php file the entire site crashed including the wordpress dashboard. I had to go through my cPanel to reupload the original Functions.php file without the redirect script. Here’s my site crashing script below, hopefully over the next couple of days I can figure out a working solution.

if( $pagename !=('home'))  {
    if (isset($_COOKIE['brands'])){
        if($_COOKIE['brands'] == 'brand_2' )
            header('Location: http://test.site.com/brand2/');
        exit;
    }
}

Here is another version I tried using all three brand cookies, which caused too many redirects in the browser when I tested it out:

function brand_redirect(){
    if($pagename !='home' ) {
        if($_COOKIE['brands'] == 'brand_1' ) 
            header('Location: http://test.site.com/brand1'); }
    elseif($pagename !='home' ) {
        if($_COOKIE['brands'] == 'brand_2' ) 
            header('Location: http://test.site.com/brand2'); }
    elseif($pagename !='home' ) {
        if($_COOKIE['brands'] == 'brand_3' ) 
            header('Location: http://test.site.com/brand3'); }
}
add_action( 'template_redirect', 'brand_redirect' );

Advertisement

Answer

Try this code. This will set a cookie only when someone lands on a brand page.

The keys of the array $brands are the page identifiers. The corresponding values are cookie that should get set on those pages.

function brand_cookies() {

    $brands = [
       'brand1' => 'brand_1',
       'brand2' => 'brand_2',
       'brand3' => 'brand_3'
    ];

    foreach ($brands as $page => $cookie) {
        if (is_page($page)) {
        setcookie('brands', $cookie, time()+3600*24*365*3, 
SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
        }
    }
}

For the second one, I think you only want to set the cookie on the home page.

function brand_redirect(){
    $brands = [
        'brand_1' => 'http://test.site.com/brand1',
        'brand_2' => 'http://test.site.com/brand2',
        'brand_3' => 'http://test.site.com/brand1'
    ];

    if($pagename =='home' && isset($_COOKIE['brands'])) { // only on home page
        foreach ($brands as $cookie => $redirect_page) {
            if ($_COOKIE['brands'] == $cookie) {
                header("Location: $redirect_page"); 
            }
        }
}
add_action( 'template_redirect', 'brand_redirect' );
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement