wp_max_upload_size()
Gets the maximum file size (in bytes) that is allowed to be uploaded in WordPress.
By default, the maximum size is taken from the PHP settings. Usually, these settings are located in the php.ini
file.
The function will take the minimum value of one of the options: upload_max_filesize
or post_max_size
. For example, with the following option values, the file size during upload should not exceed 10 MB:
[PHP] upload_max_filesize = 10M post_max_size = 100M
If the file size exceeds the maximum allowable size, we will receive such an error when trying to upload the file:
WARNING: in nginx there is a similar post_max_size
option client_max_body_size
, which can be defined for the http, server context.
If it is set to some value and it is less than the value of post_max_size
in PHP, then nginx will not allow the request, and the PHP processing will not even occur.
Therefore, if something is not working for you, check if this option is set in nginx. Set this option to zero: client_max_body_size 0;
to disable the request body size check, and then only the PHP settings will remain relevant.
For multisite builds, the hook of this function is attached to the function upload_size_limit_filter():
add_filter( 'upload_size_limit', 'upload_size_limit_filter' );
Thus, for multisite, we can limit the maximum size of the uploaded file from the admin panel (this data is stored in the option get_site_option( 'fileupload_maxk' ):
Hooks from the function
Returns
Int
. The maximum allowable file size for upload in Bytes.
Usage
wp_max_upload_size();
Examples
#1 Limiting the file size for your form
Suppose we have such a form where we have added the maximum allowed file size to the data-maxfsize
attribute:
<form> <input id="file-input" type="file" data-maxfsize="<?= (int) wp_max_upload_size() ?>" /> <p id="file-result"></p> <input id="file-submit" type="submit" disabled /> </form>
Now, we can use a JS script like this to check a file for its size:
const fileInput = document.getElementById('file-input') const fileResult = document.getElementById('file-result') const fileSubmit = document.getElementById('file-submit') const maxFileSize = parseInt( fileInput.dataset['maxfsize'] ) fileInput.addEventListener( 'change', function(){ if( fileInput.files.length > 0 ){ const fileSize = fileInput.files.item(0).size if( maxFileSize >= fileSize ){ fileSubmit.disabled = false } else{ fileResult.innerHTML = `Please select a file less than ${ Math.floor( maxFileSize / (1024 * 1024) ) } MB.` } } } );
#2 Limit the maximum file size that can be uploaded
Let's say our server allows us to upload files that are 300 MB in size. We need this for other scripts, but in WordPress we want managers to not be able to upload files larger than 10 MB.
To achieve this, we can use the upload_size_limit filter:
add_filter( 'upload_size_limit', static fn() => MB_IN_BYTES * 10 );
#3 What does the function return
var_dump( wp_max_upload_size() ); //> int(10485760)
Changelog
Since 2.5.0 | Introduced. |
wp_max_upload_size() wp max upload size code WP 6.8.1
function wp_max_upload_size() { $u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) ); $p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) ); /** * Filters the maximum upload size allowed in php.ini. * * @since 2.5.0 * * @param int $size Max upload size limit in bytes. * @param int $u_bytes Maximum upload filesize in bytes. * @param int $p_bytes Maximum size of POST data in bytes. */ return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes ); }