You should redirect as early as possible. The best hook for this is template_redirect
. Check for 2 conditions – if the page is ‘login’ and user is logged in:
function redirect_login_page() {
if( is_page( 'login' ) && is_user_logged_in() ) {
wp_redirect( home_url() );
exit;
}
}
add_action( 'template_redirect', 'redirect_login_page' );