The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Redirect Users After Login

Shared by Amit Dholiya · November 23, 2026 · @login_redirect

3 views
1 upvote
Back to Snippets

Sends users to a specific page after they log in, based on their role. Administrators go to the dashboard, while everyone else (subscribers, customers, members) lands on the homepage or a custom page such as My Account.

How to add it

  1. Open your child theme’s functions.php via Appearance > Theme File Editor, or use a snippets plugin like WPCode (Code Snippets > Add Snippet > PHP Snippet).
  2. Paste the code below at the bottom of the file or into the snippet box.
  3. Change /my-account/ to the page slug you want non-admin users to land on.
  4. Click Update File, or set the snippet to Active and click Save.
  5. Log out, then log in as a non-admin user to test the redirect.

How it works

  1. WordPress runs the login_redirect filter right after a successful login to decide where to send the user.
  2. The function first checks that a valid user object exists, so failed logins are ignored.
  3. If the user has the manage_options capability (administrators), they go to the admin dashboard.
  4. All other users are sent to the custom URL you set.
/**
 * Redirect users after login based on their role.
 *
 * @param string           $redirect_to           Default redirect URL.
 * @param string           $requested_redirect_to Requested redirect URL.
 * @param WP_User|WP_Error $user                  Logged-in user or error object.
 * @return string Modified redirect URL.
 */
function my_login_redirect_by_role( $redirect_to, $requested_redirect_to, $user ) {
    if ( ! isset( $user->roles ) || ! is_array( $user->roles ) ) {
        return $redirect_to;
    }

    if ( user_can( $user, 'manage_options' ) ) {
        return admin_url();
    }

    return home_url( '/my-account/' );
}
add_filter( 'login_redirect', 'my_login_redirect_by_role', 10, 3 );

Tested on

WordPress 6.8

Reported by the author on November 23, 2026. WPFolks has not verified this.

Not tested against WordPress 7.1.5. It may still work.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by