0

I am already logged in (with role administrator). If I try to login again with wp-login.php the form just shakes - the error message above the form remains empty. Following a suggestion on this forum I added the following code to my theme's function.php - it works: /* Create error message for new login attempt if user is already logged in */

add_filter('login_errors','login_error_message');
function login_error_message($error){
    //create error message for already logged in
    $error = "You are already logged in";
    return $error;
}
2
  • Sorry I posted too soon! This always causes the same message to be shown - even when there is another problem such as wrong name or password. I tried modifying the code as follows, but that doesn't help! Does anyone know how to solve this problem? add_filter('login_errors','login_error_message'); function login_error_message($error){ if (empty($error)) { $error = "You are already logged in"; } return $error; } – OldStick Jul 18 '15 at 9:42
  • Please add editions via an edit directly in your post. Code in comments are unreadable and everybody just ignores it. Comments in general are ignored by many. – Pieter Goosen Jul 18 '15 at 10:24
0

Instead of working around the error message I would suggest checking if user is already logged in on wp-login.php load. If user is logged in - redirect him/her to wp-admin. You can even add another check and, for example, redirect only administrators to wp-admin, redirect subscribers to site homepage etc.

add_action( 'login_head', 'wpse_redirect_login', 1 );
function wpse_redirect_login() {
    if( is_user_logged_in() ) {
        wp_redirect( admin_url() );
    }
}

Honestly, that's one of the issues I keep wondering why it isn't in Core.

2
  • 'login_head' is too late to perform a redirect as output would already have started. – Matt van Andel Aug 19 '15 at 4:24
  • Yes, my bad. It's possible to perform a late redirect with output buffering, however, it requires some extra code which I haven't provided. – Ihor Vorotnov Aug 31 '15 at 10:55
-1

You can detect whether the current user is logged in and redirect them to their profile page with the following…

add_action('wp_loaded','wp_login_redirect_logged_in_user',0);
public static function wp_login_redirect_logged_in_user(){
    global $pagenow;
    if ( in_array($pagenow, array('wp-login.php', 'wp-register.php') ) ) {
        if( is_user_logged_in() ) {
            $profile_page = get_edit_user_link( get_current_user_id() );
            wp_safe_redirect( $profile_page );
            die();
        }
    }
}

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.