When trying to login if already logged in, form just shakes - error message remains empty

admin2025-01-07  3

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;
}

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;
}
Share Improve this question asked Jul 18, 2015 at 8:40 OldStickOldStick 11 bronze badge 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 Commented Jul 18, 2015 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 Commented Jul 18, 2015 at 10:24
Add a comment  | 

3 Answers 3

Reset to default 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.

login_errors filter, which allows us to modify the error message displayed on the login page.

Inside the login_error_message function, we check if the user is already logged in using the is_user_logged_in() function

// Add this code to your theme's functions.php file or a custom plugin
add_filter('login_errors', 'login_error_message');

function login_error_message($error) {
    // Check if the user is already logged in
    if (is_user_logged_in()) {
        // Set custom error message for already logged in users
        $error = __('You are already logged in.', 'text-domain');
    }
    return $error;
}

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();
        }
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261916a777.html

最新回复(0)