Remove the admin bar from the WordPress admin backend

Hiding the WordPress admin bar programmatically on the backend

Whenever you are logged in to WordPress, there is a black admin bar that shows up above every page. On front-end pages, you can call show_admin_bar(false) in one of your plugin or theme functions to remove it. Individual users can also hide the admin bar on their account’s profile page.

All of this, however, doesn’t work if you are on a backend page. No matter what you do, the admin bar will always show up in those circumstances, unless you remove the admin bar programmatically — this article is here to show you how, as well as why you would want to do such a thing.

The code

Let’s get right to the meat-and-potatoes. Here’s the method I created for it:

// Removes the admin bar.
// Only use it for admin pages where you cannot use show_admin_bar(false).
function remove_admin_bar_hooks() {
		
	// Removes the hooks that render the admin bar.
	remove_action('template_redirect','_wp_admin_bar_init', 0);
	remove_action('admin_init','_wp_admin_bar_init');
	remove_action('before_signup_header','_wp_admin_bar_init');
	remove_action('activate_header','_wp_admin_bar_init');
	remove_action('wp_body_open','wp_admin_bar_render',0);
	remove_action('wp_footer','wp_admin_bar_render',1000);
	remove_action('in_admin_header', 'wp_admin_bar_render', 0);
		
	// Removes the admin bar class from the body tag.
	add_filter('body_class',function($wp_classes, $extra_classes) {
		// Deletes the admin-bar class from the arrays if present.
		return array_diff( 
			array_merge( $wp_classes, (array) $extra_classes ), 
			array('admin-bar')
		);
	},10000,2);
}

You’ll want to call it in one of the WordPress action hooks. I recommend admin_init.

There are 2 things happening in the method.

1. Hooks for initialising and rendering the admin bar are removed

These hooks are found in lines 608 to 616 (as of WordPress 5.8.1) of wp-includes/default-filters.php:

// Admin Bar.
// Don't remove. Wrong way to disable.
add_action( 'template_redirect', '_wp_admin_bar_init', 0 );
add_action( 'admin_init', '_wp_admin_bar_init' );
add_action( 'before_signup_header', '_wp_admin_bar_init' );
add_action( 'activate_header', '_wp_admin_bar_init' );
add_action( 'wp_body_open', 'wp_admin_bar_render', 0 );
add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); // Back-compat for themes not using `wp_body_open`.
add_action( 'in_admin_header', 'wp_admin_bar_render', 0 );

2. The admin-bar class is removed

In every WordPress page where the admin bar is rendered, the <body> tag of the page will have an admin-bar class:

<body class="home blog logged-in admin-bar no-customize-support group-blog has-thumbnail">

This class notifies themes that there is an admin bar on the page, so that themes that provide an alternative page layout to accommodate the admin bar do not use it.

When would you use this?

If you were to disable access to the WordPress admin backend for your users for any reason, you’ll find this useful. For instance, the following code prevents Subscribers from accessing wp-admin when logged in by forcing WordPress to show a 404 page instead:

add_action('admin_init', 'your_admin_init');
function your_admin_init() {
	// If a subscriber accesses the admin area.
	if(current_user_can('subscriber')) {
		// Triggers the 404 page.
		global $wp_query;
		$wp_query->set_404();
		status_header( 404 );
		get_template_part( 404 );			
		exit;
	}
}

In the above example, even though a 404 is shown, WordPress will still detect that the user is in an admin area, and show the WordPress admin bar. Using show_admin_bar(false) will not work here, because the admin bar does not get disabled in the backend.

Conclusion

I hope this article helped you with what you were looking for. As usual, leave a comment if you’ve found any mistakes or have anything valuable to add. If you like our work, you can also consider supporting us on Patreon.


Article continues after the advertisement:

Save Soil

The Save Soil Movement

Poor farming practices and policies across the world have led to the degradation of agricultural soils in the world. This has led to the loss of nutritional value in our food over the years, and it will affect our food production capabilities in the coming decades.

Learn more about Save Soil →


Leave a Reply

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

For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

I agree to these terms.