tags:

views:

46

answers:

3

Basically like cragslist. once you select city on craigslist, nexttime when you go to the site, it redirects you to the city you selected.

What I want to achieve: When a person comes to the site and selects a particular category, the next time they come to the site (returning user) - the page will open up on that category section.

I would think this would be fairly easy to do via setting a cookie when the visitor clicks on the category link (or when the category page loads). When they return the following time, the cookie is read and then the page redirects accordingly.

Unfortunately my knowledge of PHP and cookies is limited, (hence my search for answers) so I need to ask if anyone can help me out!

Anyone have any ideas?

Thanks!

A: 

Read this article on how to set and get cookies with PHP: http://www.w3schools.com/PHP/php_cookies.asp

Then read this page on PHP header redirecting: http://php.net/manual/en/function.header.php

Put the two together, and you can direct users anywhere! Good luck.

Matt Fisher
A: 

songdogtech has a good link, but WordPress has a built in function to redirect users and pass an http status code as well as it's own preferred method of settings cookies.

Give this a shot. I'm not too sure if it works, because I can't test it out right now, but it should point you in the right direction.

function user_cat()
{
    //Check to see if our cookie is set
    if(isset($_COOKIE['visitorhome']))
    {
        //Redirect to the link defined in the cookie
        wp_redirect($_COOKIE['visitorhome'], 302);
    }
    else
    {
        //If it's a category page than get the current URL set the cookie with it.
        if(is_category())
        {
            $user_cat = get_permalink();
            setcookie("visitorhome", $user_cat, time()+86400, "/", str_replace('http://www','',get_bloginfo('url')) );
        }
    }
}
add_action('init', 'user_cat');
hsatterwhite