How to Redirect Users Based on Role After Login or Logout

Common roles include:

  • Administrator
  • Editor
  • Author
  • Contributor
  • Subscriber
  • Customer (WooCommerce)
  • Shop Manager (WooCommerce)
  • Or custom roles (e.g. attendee, validator, event-manager)

To Redirect Users Based on Role After Login or Logout Use Below Code

Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

function custom_login_redirect_by_role($redirect_to, $request, $user) {
    // Ensure user is logged in
    if (isset($user->roles) && is_array($user->roles)) {
        if (in_array('administrator', $user->roles)) {
            return admin_url();
        } elseif (in_array('shop_manager', $user->roles)) {
            return site_url('/shop-dashboard/');
        } elseif (in_array('customer', $user->roles)) {
            return site_url('/my-account/');
        } elseif (in_array('subscriber', $user->roles)) {
            return site_url('/subscriber-home/');
        } elseif (in_array('attendee', $user->roles)) {
            return site_url('/download/');
        } elseif (in_array('event-manager', $user->roles)) {
            return site_url('/my-event/');
        } elseif (in_array('validator', $user->roles)) {
            return site_url('/validator/');
        } else {
            return home_url(); // Default fallback
        }
    }
    return $redirect_to;
}
add_filter('login_redirect', 'custom_login_redirect_by_role', 10, 3);

Redirect Users After Logout

You can also redirect users to a custom goodbye page like below:

function custom_logout_redirect() {
    // Get current user before logout
    $user = wp_get_current_user();
    if (!empty($user->roles)) {
        $role = $user->roles[0]; // assuming one role per user
        switch ($role) {
            case 'administrator':
                $redirect_url = site_url('/admin-logout/');
                break;
            case 'customer':
                $redirect_url = site_url('/goodbye-customer/');
                break;
            case 'attendee':
                $redirect_url = site_url('/thanks-for-attending/');
                break;
            case 'event-manager':
                $redirect_url = site_url('/event-manager-logout/');
                break;
            default:
                $redirect_url = home_url(); // fallback
        }
    } else {
        $redirect_url = home_url();
    }
    wp_redirect($redirect_url);
    exit;
}
add_action('wp_logout', 'custom_logout_redirect');

This code will

  • wp_get_current_user() fetches the logged-in user before they’re actually logged out.
  • We read the first role assigned to them and use a switch() to set a custom redirect.
  • wp_redirect() sends them to their role-specific logout page.

Role-based redirects are a powerful way to control user flow and improve UX. Whether you’re running a WooCommerce shop, an online course, or a multi-user blog—this small tweak can make a big difference.

🧠 Pro Tips for Readers

  • Use site_url() or home_url() to avoid hardcoding your domain.
  • Combine with custom login forms (e.g., wp_login_form()) for a seamless experience.

This is commonly used in:

  • WooCommerce stores (customers vs. vendors)
  • LMS platforms (students vs. instructors)
  • Membership sites (basic vs. premium users)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *