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 want users to be redirected to a custom login/registration page after they've got a taste for the site, by time passing or/and browsing a few pages.

I found this code that forces users to login before they can access the site, but im looking for something more specific.

add_action('template_redirect', 'redirect_to_login');

function redirect_to_login(){
 if(!is_user_logged_in()){
   // user not logged in so redirect them to login page
   $redirect_url = '' // you can determine where to redirect user after they login
   wp_redirect( wp_login_url($redirect_url), 301 ); exit;
 }
}

Thanks a bunch!

share|improve this question

1 Answer 1

You could hook into template_redirect and check a cookie.

Just an idea, not tested:

add_action( 'template_redirect', 't5_force_login_after_time_span' );

function t5_force_login_after_time_span()
{
    $cookie_name = 'first_visit';
    $now         = time();

    if ( is_user_logged_in() )
        return;

    if ( empty ( $_COOKIE['first_visit'] ) )
    {
        setcookie( $cookie_name, $now, $now + WEEK_IN_SECONDS );
        return;
    }

    $first_visit = (int) $_COOKIE['first_visit'];

    if ( ( $now - $first_visit ) > HOUR_IN_SECONDS )
        auth_redirect();
}

Be aware this violates the law in some countries, see the EU cookie law.

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.