How To Hide The Admin Bar in WordPress

Home » Blog » WordPress Development » How To Hide The Admin Bar in WordPress

If you want to disable the Admin Bar in WordPress, there are a couple of very easy ways to do so. Whether you want to disable it for everyone including the admins or for specific user roles, you’ll find the right snippets in this article.

Method 1: Disable Admin Bar For Everyone

If you want to disable the admin bar for all users on your website including the administrators, place this code snippet in your theme’s functions.php file:

add_filter( 'show_admin_bar', '__return_false' );

Tip: You can also place it in a custom plugin for your site. Use our plugin header generator to generate the plugin file, and then add this code snippet to it if you want to go this route.

Method 2: Disable Admin Bar For Anyone Who Isn’t Admin

If you have built an application on top of WordPress, you will want to disable the admin bar for users of your website.

This snippet will disable the admin bar for anyone who isn’t an administrator:

// Disable the admin bar for anyone who doesn't have the administrator role.
add_action('after_setup_theme', 'wpturbo_disable_admin_bar');
function wpturbo_disable_admin_bar() {
	// Check if the user is not an administrator...
	if (!current_user_can('administrator') && !is_admin()) {
		// If the user isn't an administrator, disable the admin bar.
		show_admin_bar(false);
	}
}

The above code adds a function called wpturbo_disable_admin_bar to the ‘after_setup_theme’ hook. This function checks if the currently logged-in user isn’t an administrator, and if they aren’t, then it disables the admin bar.

Method 3: Disable Admin Bar For Specific User Roles

If you have created a separate role for the users of your website’s tools or apps, then you might want to disable the admin bar for them.

To disable the admin bar for specific user roles, use this code snippet:

// Disable the admin bar for anyone who has a specific role.
add_action('after_setup_theme', 'wpturbo_disable_admin_bar_for_role');
function wpturbo_disable_admin_bar_for_role() {
	$user = wp_get_current_user(); // Get the current user.
	if ( in_array( 'custom_role_name', (array) $user->roles ) ) {
		// If the user has the role, then disable the admin bar.
		show_admin_bar(false);	
	}
}

The above code checks if the current user has the ‘custom_role_name’ role. If they do, then it disables the admin bar for them. Replace ‘custom_role_name’ with the name of the role you want to disable the admin bar for. For example, author to disable it for all Authors on your website.

Method 4: Use a Plugin To Disable The Admin Bar

If you don’t want to add custom code to your website, you can use the free Hide Admin Bar plugin. Keep in mind that it will disable the admin bar for everyone and has no option to disable it for particular roles. Use the code snippet in method 3 above if you want more control.

Leave a Reply

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

Register an account to save your snippets or go Pro to get more features.