How to Hide the Admin Toolbar Based on Roles

Why Hide the Admin Toolbar?

  • Clean frontend for users like “attendees”
  • Prevents access to dashboard or confusion
  • Better UI/UX for memberships or LMS sites
  • Helps when building client portals

To Hide the Admin Toolbar 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.

add_filter('show_admin_bar', '__return_false');

This disables the admin bar for everyone — not what we want! Let’s move on to the advanced, role-based solution.

Advanced Method: Hide Admin Bar by Role

Step 1: Create a Function That Checks User Role

function should_hide_admin_bar_for_user($user = null) {
    if (is_admin()) return false;

    if (!$user) {
        $user = wp_get_current_user();
    }

    if (empty($user->roles)) return false;

    $roles_to_hide_for = ['subscriber', 'attendee', 'customer', 'custom_role_slug']; // Add your roles here

    foreach ($user->roles as $role) {
        if (in_array($role, $roles_to_hide_for)) {
            return true;
        }
    }

    return false;
}

Step 2: Hook into show_admin_bar Filter

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

Bonus: Multisite Aware + User Meta Override

Add an optional override to allow certain users to still see the admin bar:

function should_hide_admin_bar_for_user($user = null) {
    if (is_admin()) return false;

    if (!$user) {
        $user = wp_get_current_user();
    }

    if (empty($user->roles)) return false;

    // Allow override per user
    $override = get_user_meta($user->ID, '_force_admin_bar', true);
    if ($override === 'yes') return false;

    // Check against roles
    $roles_to_hide_for = ['subscriber', 'attendee', 'customer', 'custom_role_slug'];

    foreach ($user->roles as $role) {
        if (in_array($role, $roles_to_hide_for)) {
            return true;
        }
    }
    return false;
}

Now, if you want to override the admin bar visibility per user, you can set user meta:

update_user_meta($user_id, '_force_admin_bar', 'yes');

With this advanced setup, you have full control over who sees the admin bar, support for custom roles, and the ability to override it per user. This is a great way to keep your frontend clean and focused, especially for membership, event, or LMS platforms.

If hiding the admin bar isn’t enough, redirect users who try to access /wp-admin Read this

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 *