0

How can I redirect the user to the splash page for the first week they ever visit only? This code would redirect it once every week, but I'm more after a once a day for 7 days solution:

function checkAccessed(){
if ( !isset($_COOKIE['accessed']) ) { 
    setcookie('accessed', 'yes', time() + 3600*24*7); 
    define("ACCESSED", false);
}else{
    define("ACCESSED", true);
}
}
add_action("init", "checkAccessed");

The splash page should permit the user to go to the home page between other links.

2
  • Define First Week Please..
    – Sisir
    May 19, 2014 at 5:47
  • Edited to "First week they ever visit"
    – Underfrog
    May 19, 2014 at 5:52

1 Answer 1

0

This can be achieved with Two Cookie.

  1. Saves the unix time of user visits first time. Expiration time set to a higher value, A cookie that never expires.
  2. Set another cookie that is valid for 24 hours.

Code

function wpse144762_check_accessed(){

    if( !isset($_COOKIE['first_time']) ) {
        // user visiting for the first time
        setcookie('first_time', 'yes', time() + 60 * 60 * 24 * 365 * 10); // expires in 10 years 
        define("ACCESSED", false);
    }elseif ( 
                !isset($_COOKIE['accessed']) 
                && intval($_COOKIE['first_time']) > time() - 60 * 60 * 24 * 7 
    ) { 
        setcookie('accessed', 'yes', time() + 3600*24); 
        define("ACCESSED", false);
    }else{
        define("ACCESSED", true);
    }
}
add_action("template_redirect", "wpse144762_check_accessed");

Code is not tested. But this should give you the idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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