Send Logged-In Users Away From the Login Page
When someone who is already logged in opens wp-login.php, they go straight to the dashboard (or the homepage for subscribers) instead of seeing the form again.
Opens WPIDE on
Code
add_action( 'login_init', function () {
$action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : 'login';
if ( 'login' !== $action || ! is_user_logged_in() || isset( $_REQUEST['interim-login'] ) || isset( $_REQUEST['reauth'] ) ) {
return;
}
if ( 'POST' === ( isset( $_SERVER['REQUEST_METHOD'] ) ? $_SERVER['REQUEST_METHOD'] : 'GET' ) ) {
return;
}
$fallback = current_user_can( 'edit_posts' ) ? admin_url() : home_url( '/' );
$target = isset( $_GET['redirect_to'] ) ? wp_validate_redirect( wp_unslash( $_GET['redirect_to'] ), $fallback ) : $fallback;
wp_safe_redirect( $target );
exit;
} );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.