Redirect User After Login in WordPress

The following is a code snippet that allows you to redirect users to a custom page after they login to your WordPress site.

function my_login_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        if( $user->has_cap( 'administrator' ) ) {
            $url = admin_url();
        } else {
            $url = home_url('/members-only/');
        }
    }
    return $url;
}
add_filter('login_redirect', 'my_login_redirect', 10, 3 );

Firstly, the code snippet checks to see if the user is a user, and if they are it checks to see their role name or capability. If the role name or capability is administrator, it’ll redirect the user to the admin area, or if the role name or capability is not an administrator, it’ll redirect the user to a members’ only page. You can change the page to whatever you like. You can also change the role name or capability, or add multiple role name or capability redirects.