is_admin_bar_showing()WP 3.1.0

Checks whether the "Admin bar" (toolbar) will be shown (or has already been shown). That is, whether the admin panel will be displayed for the current user. Conditional tag.

Hooks from the function

Returns

true|false. Boolean true or false. Returns false in cases:

  • if the user is not logged in;

  • if the display of the "Admin Bar" is disabled;

  • if the display of the admin bar is not provided (on the wp-login.php page)

  • if the "Admin Bar" is disabled in the code: add_filter('show_admin_bar', '__return_false');

  • if this is a request (constants are defined): XMLRPC_REQUEST or DOING_AJAX or IFRAME_REQUEST

In other cases, it returns true. In the admin area, it always returns true.

Usage

if( is_admin_bar_showing() ){
	// admin bar is displayed
}

Examples

0

#1 Check if there is an admin bar and do something

if( is_admin_bar_showing() ) {
	// code if there is a panel
}
else {
	// code if there is no panel
}

Notes

  • Global. true|false. $show_admin_bar
  • Global. String. $pagenow The filename of the current screen.

Changelog

Since 3.1.0 Introduced.

is_admin_bar_showing() code WP 6.9.1

function is_admin_bar_showing() {
	global $show_admin_bar, $pagenow;

	// For all these types of requests, we never want an admin bar.
	if ( defined( 'XMLRPC_REQUEST' ) || defined( 'DOING_AJAX' ) || defined( 'IFRAME_REQUEST' ) || wp_is_json_request() ) {
		return false;
	}

	if ( is_embed() ) {
		return false;
	}

	// Integrated into the admin.
	if ( is_admin() ) {
		return true;
	}

	if ( ! isset( $show_admin_bar ) ) {
		if ( ! is_user_logged_in() || 'wp-login.php' === $pagenow ) {
			$show_admin_bar = false;
		} else {
			$show_admin_bar = _get_admin_bar_pref();
		}
	}

	/**
	 * 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.
	 */
	$show_admin_bar = apply_filters( 'show_admin_bar', $show_admin_bar );

	return $show_admin_bar;
}