函数文档

is_admin_bar_showing()

💡 云策文档标注

概述

is_admin_bar_showing() 函数用于判断是否应显示管理工具栏,返回布尔值。它基于多种条件(如请求类型、用户登录状态和过滤器)动态决定显示状态。

关键要点

  • 函数返回布尔值,表示管理工具栏是否应显示。
  • 在 XMLRPC_REQUEST、DOING_AJAX、IFRAME_REQUEST、JSON 请求或嵌入页面时自动返回 false。
  • 在管理界面(is_admin())中始终返回 true。
  • 对于已登录用户,显示偏好通过 _get_admin_bar_pref() 获取;未登录用户或登录页面返回 false。
  • 可通过 show_admin_bar 过滤器钩子自定义显示逻辑,这是推荐的隐藏管理工具栏方式。

代码示例

if ( is_admin_bar_showing() ) {
    // do something
}

📄 原文内容

Determines whether the admin bar should be showing.

Description

For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.

Return

bool Whether the admin bar should be showing.

Source

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;
}

Hooks

apply_filters( ‘show_admin_bar’, bool $show_admin_bar )

Filters whether to show the admin bar.

Changelog

Version Description
3.1.0 Introduced.

User Contributed Notes