0

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!

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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