Skip to main content
Articles

Redirecting Logged In WordPress Users

In it’s basic form:


if ( is_user_logged_in() ) {
   // your code for logged in user 
} else {
   // your code for logged out user 
}

Redirect to a page using a function:


function add_login_check()
{
    if (is_user_logged_in()) {
        if (is_page(***Add Page ID Here*****)){
            wp_redirect('https://www.***Add URL Here***.com');
            exit; 
        }
    }
}

add_action('wp', 'add_login_check');|

Insert into theme:

<?php if ( is_user_logged_in() ) { ?>
    <a href="<?php echo wp_logout_url(); ?>">Logout</a>
<?php } else { ?>
    <a href="/wp-login.php" title="Members Area Login" rel="home">Members Area</a>
<?php } ?>

Give a personalized message for logged in users and a generic one for anonymous visitors:

function wpdocs_personal_message_when_logged_in() {
  if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    printf( 'Personal Message For %s!', esc_html( $current_user->user_firstname ) );
  } else {
    echo( 'Non-Personalized Message!' );
  }
}
add_action( 'loop_start', 'wpdocs_personal_message_when_logged_in' );

Login and Logout based on user roll:


<?php 
if ( is_user_logged_in() ) {
  echo 'Welcome, logged in user. <a href="'.wp_logout_url().'">Click here to logout</a>.';
}else{
  echo 'Please login by <a href="'.wp_login_url().'">clicking here</a>.'
}