You can redirect a user based on their WordPress role with the login_redirect filter.

add_filter( 'login_redirect', 'themeprefix_login_redirect', 10, 3 );
/**
 * Redirect user 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 themeprefix_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

In the above any admin users to go wp-admin on login every other role goes to the site home URL.

 

add_filter( 'login_redirect', 'themeprefix_login_redirect', 10, 3 );
/**
 * Redirect user 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 themeprefix_login_redirect( $redirect_to, $request, $user ){

    //is there a user to check?

    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {

            $redirect_to = '/wp-admin/'; // Your redirect URL
        }
    }

    return $redirect_to;
}

In the amended code above, any admin is forced to wp-admin as the $redirect_to variable is reset.

Share:

Leave a Reply