WordPress has many user roles, including subscriber, contributor, author, editor, and administrator; you can redirect particular users by role with the following function, which allows for customized user experiences based on their assigned capabilities and permissions.
This functionality is particularly useful when managing a large number of users, as it enables site administrators to ensure that users are directed to relevant areas of the site that match their role, enhancing both usability and security. By leveraging this method, you can create a more streamlined navigation experience that caters specifically to the needs of different user groups, ultimately improving site interaction and satisfaction.
/**
* Redirects users immediately after login based on their user role.
*
* NOTE: Using 'user_level' is deprecated. This code has been updated to use
* 'current_user_can()' with role slugs for better practice.
*
* @param string $redirect_to The default redirect URL (ignored here, but kept for future compatibility).
* @param string $requested_redirect_to The redirect URL that was originally requested (ignored here).
* @param object $user The WP_User object for the user being redirected.
* @return string The URL to redirect to.
*/
function redirect_user_on_login_by_role( $redirect_to, $requested_redirect_to, $user ) {
// Determine the redirect URL based on the user's role.
if ( user_can( $user, 'subscriber' ) ) {
// Redirect Subscribers to a specific page by title.
// NOTE: 'get_page_by_title' is a resource-intensive function.
// It's better to use the Page ID if possible for performance.
$page = get_page_by_title( 'Particular Page By Name' );
if ( $page ) {
return esc_url( get_permalink( $page->ID ) );
}
// Fallback for subscribers if the page isn't found
return home_url();
} elseif ( user_can( $user, 'contributor' ) || user_can( $user, 'editor' ) ) {
// Redirect Contributors and Editors to the site's homepage.
return home_url();
} else {
// For Administrators and other roles, send them to an external URL.
// This is typically NOT recommended; usually, admins go to the dashboard.
return 'http://google.com/';
}
// Default return (shouldn't be reached, but good practice)
return $redirect_to;
}
// Hook into the 'login_redirect' filter to control where a user goes after a successful login.
add_filter( 'login_redirect', 'redirect_user_on_login_by_role', 10, 3 );Code language: PHP (php)

Great code!
Thanks, thanks, thanks!