Change WordPress user roles and capabilities Forums How to or FAQ How do I redirect a subscriber to XYZ page after login? Reply To: How do I redirect a subscriber to XYZ page after login?

#4822
Vladimir
Keymaster

Hi,

Try this variant:


/**
 * Redirect to the home page all users except administrator after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */

function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for subscribers
        if (!in_array('administrator', $user->roles)) {
            // redirect them to another URL, in this case, the homepage 
            $redirect_to =  home_url();
        }
    }

    return $redirect_to;
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );