Redirect User After Login in WordPress

Have you ever wanted to redirect users to a specific page after they login to your WordPress site?

By default, WordPress either takes users to the admin dashboard or their profile section in the WordPress admin area, depending on their user role.

The great news is, it’s super easy to redirect users somewhere else when they login. You can even set conditionals to redirect different users to different places.

Redirect all users

First of all, we’ll have a look at how to do a simple redirect for all users. In the example below, whenever somebody logs in to our WordPress site they’ll be redirected to the page that we specify. Just replace custom-page with whatever URL you need:

function snippetpress_login_redirect() {
    return '/custom-page'; // Replace custom-page with your URL.
}
add_filter( 'login_redirect', 'snippetpress_login_redirect' );

The above example assumes that the page we are redirecting to is on the same WordPress site. If you need to redirect users to a different site, you’ll need to include the full URL, like below:

function snippetpress_login_redirect() {
    return 'http://custom.url/custom-page';
}
add_filter( 'login_redirect', 'snippetpress_login_redirect' );

We can also easily redirect all users to the homepage of our WordPress site:

function snippetpress_login_redirect() {
    return get_home_url();
}
add_filter( 'login_redirect', 'snippetpress_login_redirect' );

Redirect certain users

What if we only want to redirect subscribers? Or even, only redirect specific users? Well, that’s pretty simple too, we just need to use some conditionals. In the example below we’re going to redirect subscribers to a custom URL, but leave everyone else as default:

function snippetpress_login_redirect( $redirect, $request, $user ) {
	
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		
		// Check for subscribers
		if ( in_array( 'subscriber', $user->roles ) ) {
			$redirect = '/custom-page';
		}
		
	}	
	
	return $redirect;
  
}

add_filter( 'login_redirect', 'snippetpress_login_redirect', 9999, 3 );

If we want to redirect specific users to a certain page, then we’ll need to know their user IDs. In the following example, we’re going to redirect the users with IDs 11, 28 and 82 to our custom page, and leave everyone else to default:

function snippetpress_login_redirect( $redirect, $request, $user ) {
	
	// Here we specify the user IDs that we want to redirect
	$redirect_users = array(
		11,
		28,
		82,
	);
	
	if ( isset( $user->ID ) ) {
		
		// Check for our users
		if ( in_array( $user->ID, $redirect_users ) ) {
			$redirect = '/custom-page';
		}
		
	}	
	
	return $redirect;

}

add_filter( 'login_redirect', 'snippetpress_login_redirect', 9999, 3 );

Where to add the snippet?

Whichever snippet you choose to use, you should place it at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.