upload_size_limit
云策文档标注
概述
upload_size_limit 是一个 WordPress 过滤器,用于修改 PHP.ini 中允许的最大上传文件大小。它允许开发者基于特定条件(如用户权限)动态调整上传限制。
关键要点
- 过滤器名称:upload_size_limit
- 参数:$size(当前上传大小限制,以字节为单位)、$u_bytes(最大上传文件大小)、$p_bytes(最大 POST 数据大小)
- 返回值:应用过滤器后,返回 $u_bytes 和 $p_bytes 中的较小值
- 相关函数:wp_max_upload_size() 用于确定 PHP.ini 中的最大上传大小
- 引入版本:WordPress 2.5.0
代码示例
/**
* Filter the upload size limit for non-administrators.
*
* @param string $size Upload size limit (in bytes).
* @return int (maybe) Filtered size limit.
*/
function filter_site_upload_size_limit( $size ) {
// Set the upload size limit to 10 MB for users lacking the 'manage_options' capability.
if ( ! current_user_can( 'manage_options' ) ) {
// 10 MB.
$size = 1024 * 10000;
}
return $size;
}
add_filter( 'upload_size_limit', 'filter_site_upload_size_limit', 20 );注意事项
- add_filter 的第三个参数是回调函数的优先级,默认为 10
- 过滤器可以用于基于用户角色或能力自定义上传限制
原文内容
Filters the maximum upload size allowed in php.ini.
Parameters
$sizeint-
Max upload size limit in bytes.
$u_bytesint-
Maximum upload filesize in bytes.
$p_bytesint-
Maximum size of POST data in bytes.
Source
return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
Skip to note 3 content
Drew Jaynes
/** * Filter the upload size limit for non-administrators. * * @param string $size Upload size limit (in bytes). * @return int (maybe) Filtered size limit. */ function filter_site_upload_size_limit( $size ) { // Set the upload size limit to 10 MB for users lacking the 'manage_options' capability. if ( ! current_user_can( 'manage_options' ) ) { // 10 MB. $size = 1024 * 10000; } return $size; } add_filter( 'upload_size_limit', 'filter_site_upload_size_limit', 20 );Skip to note 4 content
Jake Spurlock
The third parameter is the priority to call the callback function. 10 is the default.