get_space_used()
云策文档标注
概述
get_space_used() 函数用于获取当前站点已使用的存储空间,以兆字节(MB)为单位返回。它通过计算上传目录的大小来实现,并提供了一个过滤器钩子允许自定义空间使用量。
关键要点
- 返回当前站点已使用的存储空间,单位为兆字节(MB)。
- 使用 wp_upload_dir() 获取上传目录路径,并通过 get_dirsize() 计算目录大小。
- 包含 apply_filters('pre_get_space_used', $space_used) 钩子,允许过滤或覆盖空间使用量。
- 函数在 WordPress 3.5.0 版本中引入。
代码示例
function get_space_used() {
$space_used = apply_filters( 'pre_get_space_used', false );
if ( false === $space_used ) {
$upload_dir = wp_upload_dir();
$space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES;
}
return $space_used;
}注意事项
- 函数返回整数类型,表示已使用的兆字节数。
- 如果过滤器返回非 false 值,将直接使用该值,否则通过计算上传目录大小来获取。
- 相关函数包括 wp_upload_dir()、get_dirsize() 和 apply_filters(),用于辅助计算和过滤。
原文内容
Returns the space used by the current site.
Source
function get_space_used() {
/**
* Filters the amount of storage space used by the current site, in megabytes.
*
* @since 3.5.0
*
* @param int|false $space_used The amount of used space, in megabytes. Default false.
*/
$space_used = apply_filters( 'pre_get_space_used', false );
if ( false === $space_used ) {
$upload_dir = wp_upload_dir();
$space_used = get_dirsize( $upload_dir['basedir'] ) / MB_IN_BYTES;
}
return $space_used;
}
Hooks
- apply_filters( ‘pre_get_space_used’, int|false $space_used )
-
Filters the amount of storage space used by the current site, in megabytes.
Changelog
| Version | Description |
|---|---|
| 3.5.0 | Introduced. |