WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question

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.

share|improve this answer

Your Answer


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.