Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a tricky situation.... I'd like to check for a cookie. If it doesn't exist then then redirect to an internal wordpress page, and set a cookie. And then carry on browsing the site. But i get stuck in a loop if the url doesn't exist. This is what i have so far...any help would be great.

function cookiebasedredirect() {

    // WHEN YOU HAVE FOUND YOUR COOKIE
    if ( !isset($_COOKIE["sevisitor"])) {

        setcookie('sevisitor', 1, time()+1209600, "/", "http://localhost/child/", false);       

        // GRABS THE CURRENT PAGE NAME - THIS IS ALSO KNOWS AS THE PAGE/POST SLUG
        $pagename = get_query_var('pagename');

        // PAGE CHECK SO THAT YOU ARE NOT IN AN INFINITE LOOP
        // IN THIS SAMPLE MEDIA-GALLERIES IS THE PAGE YOU WANT TO BE 
        //  REDIRECTED TO IF A COOKIE IS NOT SET, BUT ONCE YOU GET THERE
        // MAKE SURE WORDPRESS DOESN'T EXECUTE THE REDIRECT
        if( $pagename != "about-myself") {          
            wp_redirect( get_site_url().'/about-myself' ); exit;

        } else {


        }


    } else {

    }}
add_action("template_redirect", "cookiebasedredirect");
share|improve this question

2 Answers 2

up vote 2 down vote accepted

why not use the init action hook:

function has_my_cookie()
{
    if (!is_admin()){
        //Check to see if our cookie is set if not redirect to your desired page and set the cookie
        if ( !isset($_COOKIE["sevisitor"])) {
            //setcookie
            setcookie('sevisitor', 1, time()+1209600, "/", "http://localhost/child/", false);
             //Redirect 
            wp_redirect( get_site_url().'/about-myself' ); exit;
        }
    }
}
add_action('init', 'has_my_cookie');
share|improve this answer
    
I would move the setcookie from within the if statement, if the OP intention is to make the first page only show up on first visit. Otherwise, the user will get the page every two weeks. –  Mild Fuzz Aug 4 '11 at 9:58
1  
Or just change the cookie life time to something longer –  Bainternet Aug 4 '11 at 14:53
    
yeah, you could. I like my method because it treats a user as new user if they've been gone for a relatively long time. But it's potatoes poh-tar-toes. –  Mild Fuzz Aug 4 '11 at 15:44
    
Thanks for the help guys, i'm still trying to suss this out...I still get stuck in a redirect loop. It's definitely seems like the code should work...but i can't seem to get it to work...hmm... –  Wok Aug 5 '11 at 22:30
    
i can't see how you get a redirect loop unless you don't have cookies enabled. –  Bainternet Aug 5 '11 at 23:55

My final solution...

function cookie_redirect() {

    // THE PAGE SLUG, YOU WANT TO BE REDIRECTED TO, WHEN THERE IS NO COOKIE
    $pageslug = "about-myself";

    // THE COOKIE NAME
    $cookie_name = "thecookiemonster";

    // CHECK IF YOUR COOKIE IS SET
    if (!isset($_COOKIE[$cookie_name])) {

       // SINCE THERE IS NO COOKIE, THEN SET IT
       setcookie( $cookie_name, 1, time()+1209600, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);

        // GRABS THE CURRENT PAGE NAME - THIS IS ALSO KNOWN AS THE PAGE/POST SLUG
        $pagename = get_query_var('pagename');

        // MAKE SURE YOU ARE NOT AT THE PAGE YOU WANT TO BE DIRECTED TO, PREVENTS LOOP
        if( $pagename != $pageslug) {
            // REDIRECT US
            wp_redirect( get_site_url() . '/' . $pageslug ); exit;
        } else {

        }

    } else {


    }
}
add_action( 'init', 'cookie_redirect');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.