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(){
   // 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!

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() )

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

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

    if ( ( $now - $first_visit ) > HOUR_IN_SECONDS )

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 and acknowledge you have read our privacy policy.

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