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