Users & Login PHP Intermediate

Redirect After Login Based on User Role

Send users to different URLs after login depending on their role, administrators go to the dashboard, customers go to My Account, and subscribers go to a custom page.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 26 lines
add_filter( 'login_redirect', function ( string $redirect_to, string $requested, $user ): string {
	// Failed logins pass a WP_Error, not a user.
	if ( ! $user instanceof WP_User ) {
		return $redirect_to;
	}

	// Respect a specific page the user was trying to reach.
	if ( $requested && $requested !== admin_url() ) {
		return $redirect_to;
	}

	$roles = (array) $user->roles;

	if ( array_intersect( [ 'administrator', 'editor' ], $roles ) ) {
		return admin_url();
	}
	if ( in_array( 'author', $roles, true ) ) {
		return admin_url( 'post-new.php' );
	}
	if ( in_array( 'customer', $roles, true ) && function_exists( 'wc_get_page_permalink' ) ) {
		return wc_get_page_permalink( 'myaccount' );
	}

	// Everyone else goes to the front end.
	return home_url( '/' );
}, 10, 3 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress sends every user to the admin dashboard after login by default, regardless of whether that user has any reason to be in the admin. WooCommerce customers, membership site subscribers, and front-end-only users all land in /wp-admin/ after logging in, which is disorienting when you’ve built a front-end member area or account portal for them. The login_redirect filter lets you route each user role to the appropriate destination immediately after authentication.

What the code does

The filter receives three arguments: the current redirect destination, the requested redirect URL from the login form, and the WP_User object of the user who just logged in.

Honouring Explicit Redirects

The first check respects any explicit redirect_to parameter that was passed to the login URL. This covers the common case where a user was redirected to login because they tried to access a specific page, after logging in, they should land on that page rather than whatever role-based destination the snippet would send them to. The check compares the requested redirect against the plain admin URL, if it’s different, something explicitly requested that destination and should be honoured.

Role-Based Routing

The routing logic uses array_intersect() for roles where multiple roles should share the same destination, administrators and editors both going to the dashboard in the example. in_array() with strict comparison handles roles that map to a single destination.

The order of the role checks matters. PHP evaluates them top to bottom, and a user with multiple roles (which is possible) will match the first applicable condition. Place the most specific or highest-privilege role checks first so they take priority over broader conditions at the bottom.

WooCommerce My Account URL

wc_get_page_permalink( 'myaccount' ) returns the URL of the WooCommerce My Account page as configured in WooCommerce settings. Using this function rather than hardcoding the URL ensures the redirect works correctly even if the My Account page URL is changed in the admin. The function is only available when WooCommerce is active, the snippet doesn’t include a WooCommerce existence check, so add one if WooCommerce is not guaranteed to be active on your site.

Fallback Destination

The final return at the bottom is the catch-all for any role not explicitly handled above, subscribers, custom roles added by plugins, and any future roles. Replace home_url( '/members/' ) with whatever front-end URL makes sense for your lowest-privilege users, a dashboard page, a welcome page, or the homepage.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only