How To Determine WordPress Admin Bar Is Visible Or Not

geekerhub

Updated on:

admin bar is visible or not

To determine whether the admin bar is visible or not, we need to use the filter name show_admin_bar. Put the below code snippet in your active theme’s functions.php file to sets the display status of the admin bar.

To determine if the WordPress admin bar is visible, you can use a simple conditional check in your theme or plugin code. One way is to utilize the is_admin_bar_showing() function, which returns true if the admin bar is visible and false if it’s not. You can incorporate this function in your code logic to perform specific actions or display different content based on the admin bar’s visibility status.

function show_admin_bar_callback( $show_admin_bar ){
	if( $show_admin_bar ){
		//Admin bar is showing, do some logic here
	}
	//Admin bar is not showing, do some logic here

	return $show_admin_bar;
}
	
add_filter('show_admin_bar', 'show_admin_bar_callback' );

To hide the WordPress admin bar, just need to return false to the show_admin_bar filter.

/**
 * Filters whether to show the admin bar.
 *
 * Returning false to this hook is the recommended way to hide the admin bar.
 * The user's display preference is used for logged in users.
 *
 * @since 3.1.0
 *
 * @param bool $show_admin_bar Whether the admin bar should be shown. Default false.
 */

 add_filter('show_admin_bar', false );

show_admin_bar() can be called immediately upon plugin load.

This function will also affect the display of the toolbar in the dashboard for WordPress versions prior to Version 3.3.

The init action is fired before wp and admin_init. It’s possible to use show_admin_bar() inside the init action, but other plugins might overwrite your decision at a later point.

It is not recommended to use the filter add_filter( 'show_admin_bar', '__return_false' ); in shared plugins!

It will create conflicts with other plugins. Only use the filter in your private (child) theme or internal plugins, and stick with the show_admin_bar() function in public or shared plugins.


What more you can do with this awesome filter?

You can do many tricks with the Admin Bar filter like as mentioned below:

  • Hiding Admin Bar Individual Users, All Users, Non-Admin
  • You can always show the Admin Bar
  • You can change the position of the Admin Bar

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with the Storefront theme, the WooCommerce version listed above, and WordPress-friendly hosting on PHP 7.3. Thank you in advance 🙂

Leave a Comment