函数文档

is_blog_admin()

💡 云策文档标注

概述

is_blog_admin() 函数用于判断当前请求是否针对 WordPress 站点的管理界面(如 /wp-admin/)。它不验证用户权限,仅检查请求上下文。

关键要点

  • 检查当前请求是否在站点管理页面,返回布尔值 true 或 false。
  • 不检查用户是否为管理员;如需验证角色或能力,应使用 current_user_can() 函数。
  • 函数内部通过 $GLOBALS['current_screen']->in_admin('site') 或常量 WP_BLOG_ADMIN 实现判断。

代码示例

function is_blog_admin() {
	if ( isset( $GLOBALS['current_screen'] ) ) {
		return $GLOBALS['current_screen']->in_admin( 'site' );
	} elseif ( defined( 'WP_BLOG_ADMIN' ) ) {
		return WP_BLOG_ADMIN;
	}

	return false;
}

注意事项

  • 自 WordPress 3.1.0 版本引入。
  • 相关函数包括 wp_dashboard_setup()、wp_admin_bar_site_menu() 和 wp_validate_logged_in_cookie()。

📄 原文内容

Determines whether the current request is for a site’s administrative interface.

Description

e.g. /wp-admin/

Does not check if the user is an administrator; use current_user_can() for checking roles and capabilities.

Return

bool True if inside WordPress site administration pages.

Source

function is_blog_admin() {
	if ( isset( $GLOBALS['current_screen'] ) ) {
		return $GLOBALS['current_screen']->in_admin( 'site' );
	} elseif ( defined( 'WP_BLOG_ADMIN' ) ) {
		return WP_BLOG_ADMIN;
	}

	return false;
}

Changelog

Version Description
3.1.0 Introduced.