Send each type of user to the right place after they log in, and hide the toolbar from users who should not see it. Both are common in membership sites and online stores.

Redirect after login based on the user role

function lotgrafix_login_redirect( $redirect_to, $request, $user ) {
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        if ( in_array( 'administrator', $user->roles, true ) ) {
            return admin_url();
        }

        if ( in_array( 'customer', $user->roles, true ) ) {
            return home_url( '/my-account/' );
        }

        return home_url( '/dashboard/' );
    }

    return $redirect_to;
}
add_filter( 'login_redirect', 'lotgrafix_login_redirect', 10, 3 );

The check for $user->roles is needed because the filter also runs when the login fails and $user is an error object.

Redirect after logout

function lotgrafix_logout_redirect() {
    wp_safe_redirect( home_url() );
    exit;
}
add_action( 'wp_logout', 'lotgrafix_logout_redirect' );

Hide the admin bar for everyone except administrators

function lotgrafix_show_admin_bar( $show ) {
    return current_user_can( 'manage_options' ) ? $show : false;
}
add_filter( 'show_admin_bar', 'lotgrafix_show_admin_bar' );

Block non administrators from the dashboard

function lotgrafix_block_admin_access() {
    if ( is_admin() && ! current_user_can( 'manage_options' ) && ! wp_doing_ajax() ) {
        wp_safe_redirect( home_url() );
        exit;
    }
}
add_action( 'admin_init', 'lotgrafix_block_admin_access' );

Keep the wp_doing_ajax() check. Without it, front end features that use admin-ajax.php will stop working for regular users.

Notes

  • Use wp_safe_redirect() to avoid sending users to untrusted sites.
  • Always call exit right after a redirect.
  • Role names like customer and subscriber depend on your site and plugins.
«
»