Redirect users to different pages after login based on their WordPress role.
This snippet sends administrators to the WordPress dashboard while redirecting all other users to a custom account page. Useful for membership sites, WooCommerce stores, client portals, and custom user experiences.
Features
- Redirect admins to
/wp-admin/ - Redirect subscribers, customers, and other roles to a custom page
- Uses the
login_redirectfilter - Lightweight and easy to customize
Best for
- WooCommerce stores
- Membership websites
- Client dashboards
- Custom user portals
function custom_login_redirect($redirect_to, $request, $user) {
// Check if user logged in successfully
if (isset($user->roles) && is_array($user->roles)) {
// Redirect based on role
if (in_array('administrator', $user->roles)) {
return admin_url();
} else {
return home_url('/my-account/');
}
}
return $redirect_to;
}
add_filter('login_redirect', 'custom_login_redirect', 10, 3);