Do you want to redirect the user after they log into your WordPress site? There’s an easy way to do this. You can also redirect different users to different pages on your website. Maybe you have a custom user role that you don’t want to redirect to the WordPress admin page. You can redirect them to a custom page easily using a simple code snippet.

If you want to redirect the user to a custom page based on the user’s capability, then add this code snippet to your theme’s functions.php file:

function redirect_user_after_login() {

	// Check if the user has the capability to edit posts.
	if ( current_user_can('edit_posts') ){
		return '/custom-page'; // The URL of the page you want to redirect to.
	}
	
}
add_filter('login_redirect', 'redirect_user_after_login');

The above code will redirect the user to a custom page if the user has the edit_posts capability. Replace edit_posts with any other capability you want to redirect based on.

If you want to redirect the user based on their role, then add this code to your functions.php file:

function redirect_user_after_login() {

	// Get the current user.
	$user = wp_get_current_user();
	
	// Check if the user has the author role or any other custom role.
	if ( in_array( 'author', (array) $user->roles ) ) {
		return '/author-page'; // The URL you want to redirect authors to.
	}

}
add_filter('login_redirect', 'redirect_user_after_login');

The above code first gets the current user and then checks if the current user has ‘author’ as one of their roles. If the user has author as a role, then the user will be redirected to the custom page. Replace author with any other role you want to redirect based on. You can enter custom roles or primitive roles that are built into WordPress.

If you want to add many different redirections based on different conditions, add all those conditions inside the redirect_user_after_login function:

function redirect_user_after_login() {

	// Check if the user has the capability to edit posts.
	if ( current_user_can('edit_posts') ){
		return '/custom-page'; // The URL of the page you want to redirect to.
	}

	// Get the current user.
	$user = wp_get_current_user();
	
	// Check if the user has the author role or any other custom role.
	if ( in_array( 'author', (array) $user->roles ) ) {
		return '/author-page'; // The URL you want to redirect authors to.
	}

	// Check if the user has the capability to administrator.
	if ( current_user_can('administrator') ){
		return '/custom-page'; // The URL of the page you want to redirect to.
	}

}
add_filter('login_redirect', 'redirect_user_after_login');

You can add as many conditions to the above code as you want.

Pin It on Pinterest

Share This