is_super_admin()
云策文档标注
概述
is_super_admin() 函数用于判断用户是否为站点管理员,支持单站点和多站点环境。它接受一个可选的用户ID参数,默认检查当前用户。
关键要点
- 参数 $user_id 可选,默认为 false,表示检查当前用户;传入用户ID则检查指定用户。
- 返回值类型为布尔值,true 表示用户是站点管理员,false 表示不是。
- 在多站点环境中,通过检查用户登录名是否在超级管理员列表中判断;在单站点环境中,通过检查用户是否拥有 delete_users 能力判断。
- 函数内部使用 wp_get_current_user()、get_userdata()、is_multisite() 和 get_super_admins() 等辅助函数。
- 自 WordPress 4.8 起,建议使用 current_user_can('setup_network') 替代 is_super_admin() 进行网络能力检查。
代码示例
// As of WordPress 4.8 the use of is_super_admin is discouraged. Use the setup_network capability check instead.
if ( current_user_can( 'setup_network' ) ) {
// do something
}注意事项
- 如果用户不存在或 $user_id 无效,函数返回 false。
- 在多站点中,超级管理员列表通过 get_super_admins() 获取,需确保其正确配置。
- 此函数自 WordPress 3.0.0 引入,相关替代方法需注意版本兼容性。
原文内容
Determines whether user is a site admin.
Parameters
$user_idint|falseoptional-
The ID of a user. Defaults to false, to check the current user.
Default:
false
Source
function is_super_admin( $user_id = false ) {
if ( ! $user_id ) {
$user = wp_get_current_user();
} else {
$user = get_userdata( $user_id );
}
if ( ! $user || ! $user->exists() ) {
return false;
}
if ( is_multisite() ) {
$super_admins = get_super_admins();
if ( is_array( $super_admins ) && in_array( $user->user_login, $super_admins, true ) ) {
return true;
}
} elseif ( $user->has_cap( 'delete_users' ) ) {
return true;
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 4 content
John Russell
<?php // As of WordPress 4.8 the use of is_super_admin is discouraged. Use the setup_network capability check instead. if ( current_user_can( 'setup_network' ) ) { // do something } ?>Skip to note 5 content
Codex
Example
Skip to note 6 content
tenebralyo
" . $tag . "</pre>"; $debug_tags[] = $tag; } ); } ?>