Remove the admin bar from the WordPress admin backend

Remove the admin bar on the WordPress admin backend

If you, for some reason, are trying to remove the WordPress admin bar on the backend using show_admin_bar(false) to no avail, that’s because the method does not remove the admin bar on the WordPress admin backend by design. If you still want to remove it, then this article is for you.

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:


Leave a Reply

Your email address will not be published.