get_user_count()
云策文档标注
概述
get_user_count() 函数用于返回 WordPress 安装中的活跃用户数量。在大型站点上,该计数可能被缓存,每日仅更新两次。
关键要点
- 函数返回当前网络或指定网络的活跃用户数,类型为整数
- 参数 $network_id 可选,默认为 null,表示当前网络;在非多站点环境下传递此参数会触发 _doing_it_wrong() 警告
- 内部通过 get_network_option() 获取 'user_count' 选项值,默认返回 -1
- 适用于多站点环境,但也可用于单站点
代码示例
// 获取当前站点的用户数并输出
$user_count = get_user_count();
echo "There are currently $user_count users on this site.";注意事项
- 在大型站点上,用户计数可能被缓存,更新频率较低,需注意数据实时性
- 非多站点环境下不应传递 $network_id 参数,否则会触发错误提示
- 函数自 WordPress 4.8.0 引入,6.0.0 版本移至 wp-includes/user.php
原文内容
Returns the number of active users in your installation.
Description
Note that on a large site the count may be cached and only updated twice daily.
Parameters
$network_idint|nulloptional-
ID of the network. Defaults to the current network.
Default:
null
Source
function get_user_count( $network_id = null ) {
if ( ! is_multisite() && null !== $network_id ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: $network_id */
__( 'Unable to pass %s if not using multisite.' ),
'<code>$network_id</code>'
),
'6.0.0'
);
}
return (int) get_network_option( $network_id, 'user_count', -1 );
}
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | MU (3.0.0) |
| 6.0.0 | Moved to wp-includes/user.php. |
| 4.8.0 | Introduced. |
Skip to note 3 content
Rami Yushuvaev
printf( /* translators: %s: User count */ __( 'There are currently %s users on this site.', 'text-domain' ), get_user_count() );Skip to note 4 content
Codex
Get number of active users
$user_count = get_user_count(); echo "There are currently $user_count users on this site.";