Support » Everything else WordPress » Remember user choice on next session

  • I have a splash page that asks the user which part of the site they would like to visit. They have two options -> the SHOP side or the CORPORATE side. Can anyone advise how I can remember the user’s choice so that they are not asked this each time they visit the website? I’m assuming this would need to be done via a cookie? I haven’t worked with cookies on WordPress before so would appreciate any guidance on this 🙂
    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    Yes, if you’re not going to have users login, the best way to save data is via cookie. Some examples here: https://wpengine.com/resources/wordpress-cookies/

    Thread Starter gareth94

    (@atebol)

    Thank you for your help @sterndata. That tutorial is very useful! The only thing I’m unsure of now though is how to remember the user’s page choice. If the cookie is set I’d like it to direct to the SHOP or CORPORATE side of the site (depending on their selection)

    Moderator Steve Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    You’d need to test the cookie value on your home page, either by using a plugin or putting code into the homepage to redirect based on its value. You may be able to use something off-the-shelf: https://wordpress.org/plugins/redirect-by-cookie/, for example

    Thread Starter gareth94

    (@atebol)

    Thank you Steven. I’m looking to do it in the code. I’ve gone into functions.php in my theme directory. I found the code below online (which I’ve adapted slightly) that seems to be close to what I need. I’m not sure exactly what I need changing to make it work though! 🙁

    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/website/homepage", false);
                 //Redirect 
                wp_redirect( get_site_url().'/shop' ); exit;
            }
     if ( !isset($_COOKIE["sevisitor2"])) {
                //setcookie
                setcookie('sevisitor2', 1, time()+1209600, "/", "http://localhost/website/homepage", false);
                 //Redirect 
                wp_redirect( get_site_url().'/corporate' ); exit;
            }
        }
    }
    add_action('init', 'has_my_cookie');
    • This reply was modified 10 months, 1 week ago by gareth94.
    • This reply was modified 10 months, 1 week ago by gareth94.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Remember user choice on next session’ is closed to new replies.