In my website there are many authors who can publish posts but some of them are uploading images bigger than 1M , and I want to reduce the max upload size to 500 kb.


1 Answer 1


You can forbid uploads of a specific size (or for other reasons) in the wp_handle_upload_prefilter hook that is used in the wp_handle_upload() function.

It get's the file array passed, it's a single item in the PHP standard superglobal $_FILES one that is documented in the PHP Manual: POST method uploads.

Just create a function and add it to the filter. Inside your hook, check for the filesize and set $file['error'] to your error message like "Files larger than X bytes are prevented from uploads".

add_filter('wp_handle_upload_prefilter', function($file) {
    $size = $file['size'];
    if ($size > 500 * 1024) {
       $file['error'] = '"Files larger than X bytes are prevented from uploads.';
    return $file;

This method does not technically permit the upload which means, your server is still receiving the upload data of the file from the users browser. It's just thrown away afterwards.

To prevent uploads of certain size at the server level, you need to configure your server accordingly for that which depends on what you use.

  • wow thank you very much. It is right. it is work in local server but in my website hostgator there is error 'Parse error: syntax error, unexpected T_FUNCTION in /home/***/public_html/wp-content/themes/***/functions.php on line 17"
    – AboSami
    Feb 15, 2011 at 11:12
  • @AboSami - That's because on hostgator you're using PHP 5.2 or so but the syntax I use is making use of anonymous functions as featured in PHP 5.3. Replace it with some PHP 5.2 compatible callback instead. The rest of the code should just work the same, it's more or less a way of writing.
    – hakre
    Feb 15, 2011 at 11:14
  • I don't know about using php5.3 if you know please , any way thank you very much MR hakre. ^_^
    – AboSami
    Feb 15, 2011 at 11:19
  • @AboSami - Changes between PHP 5.2 and PHP 5.3 are outlined already.
    – hakre
    Feb 15, 2011 at 11:22
  • hakre's code above worked fine for me. Only error in code is extra " mark at begining of the line - Files larger than X bytes are prevented from uploads.
    – Hrishi
    Nov 11, 2012 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.