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
- Open your child theme’s
functions.phpvia Appearance > Theme File Editor, or use a snippets plugin like WPCode (Code Snippets > Add Snippet > PHP Snippet). - Paste the code below at the bottom of the file or into the snippet box.
- Change
/my-account/to the page slug you want non-admin users to land on. - Click Update File, or set the snippet to Active and click Save.
- Log out, then log in as a non-admin user to test the redirect.
How it works
- WordPress runs the
login_redirectfilter right after a successful login to decide where to send the user. - The function first checks that a valid user object exists, so failed logins are ignored.
- If the user has the
manage_optionscapability (administrators), they go to the admin dashboard. - 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.
