Desired Outcome
I am developing a plugin that should redirect the user away from log in page if they are already logged in.
So if anyone goes to example.com/wp-login.php and they are already logged in, the redirect should kick in.
Current Progress
I am using login_init
hook to determine that we are in the log in page and wp_get_current_user()
to get the user.
function redirect_logged_in_users() {
if (is_user_logged_in()) {
wp_redirect($destination);
}
}
add_action('login_init', 'redirect_logged_in_users');
The Problem
This works well in any other page using some other hook, like init
, but whenever I try this with login_init
the wp_get_current_user()
returns an empty user as if no one is logged in.
I know that there's a user logged in.
Additional Context
I am not a WordPress dev and I am almost sure that I am doing something in a non-WordPress way. Happy to take any other approach which helps to achieve the goal.