How to Limit the Image Upload Size in WordPress

How to Limit the Image Upload Size in WordPress

If you’ve been blogging for even just a few years, you’ve probably uploaded hundreds upon hundreds of images to your WordPress site. It’s no secret that pictures and other media comprise a huge and disproportionate amount of the bandwidth of a typical webpage. For this reason, we all try and use image optimization services to convert our formats and minimize this impact. However, what if you have someone else working on your blog? Sometimes several writers will contribute to a site and it’s important for you to have control over their actions.

Some images when unmodified take up several MB of space. If this is allowed to go unchallenged, the size of your webpages will increase unchecked. You have to manually verify each and every image file to make sure that it complies with your requirements. True – WordPress often has an inbuilt file upload restriction of 2 MB, even though that can be changed by making modifications to the php.ini file. But two MB is still a large amount of data, especially if your pages are being loaded thousands of times every day.

What we need is a technique to prevent files from being uploaded that are bigger than a certain size. This process can be modified to implement other restrictions such as image dimensions. But in this article, we’re going to take an arbitrary limit of say 200 KB and write some code that generates an error whenever an image larger than this is uploaded.

Limiting Uploads from the WordPress Editor

Using the standard WordPress editor, anyone can simply upload an image file that they’ve downloaded from the web or generated themselves. When you do this, the size of the file is displayed along with a thumbnail of the image on the right-hand side of the pop-up lightbox as shown here:

image size

In the example above, the image I’ve chosen is 250 KB. Let’s see what we can do to prevent this from being uploaded and generate an appropriate error message in response. Open up your functions.php file and paste the following at the bottom before the closing ?> PHP tag:

function whero_limit_image_size($file) {

// Calculate the image size in KB
$image_size = $file['size']/1024;

// File size limit in KB
$limit = 200;

// Check if it's an image
$is_image = strpos($file['type'], 'image');

if ( ( $image_size > $limit ) && ($is_image !== false) )
        $file['error'] = 'Your picture is too large. It has to be smaller than '. $limit .'KB';

return $file;

}
add_filter('wp_handle_upload_prefilter', 'whero_limit_image_size');

In this code, I’m utilizing the wp_handle_upload_prefilter filter which allows us to intercept the request and set an error message if necessary. If you don’t know how to add custom code to your WordPress installation, see my earlier tutorial on how to do this. In this function, we calculate the image size by using the file parameter passed to us and also check whether the uploaded document is an image or not.

The section in bold indicates the code where you specify your own size limit in KB. I’ve chosen the low number of 200, and you’ll probably want to change it to something else. If the image exceeds our limit, we set an error parameter with a custom error message telling the user to upload a smaller file below it. Save your functions.php and try uploading something larger than that. If all goes well, you should see an error message like the one below:

error message

There are many other parameters that we can use to restrict image uploads – dimensions for example. Either way, the “wp_handle_upload_prefilter” filter should be the hook you use which will allow you to set an appropriate error message.

5 Comments on “How to Limit the Image Upload Size in WordPress”!

  1. Hi,

    Thank you for sharing the code, It works great.
    I wonder if is possible to set the exception for the admin who would be able to upload images without limits.
    Kind Regards

    • Hi,
      You would need to wrap the code in:
      // exclude admins
      if ( ! current_user_can( 'manage_options' ) ) {
      if ( ( $image_size > $limit ) && ($is_image !== false) ) {
      $file['error'] = sprintf( __( 'ERROR: File size limit is %d KB.' ), $limit );
      }
      }

  2. Hi Bhagwad,

    This is good info as the WP Codex doesn’t have much on the topic.

    (1) Where can I find more on the parameters and (2) could you suggest how to apply a conditional on a per upload basis rather than all uploads? Eg, if current_user_can (capability) do whero_limit_image_size.

    I want to limit the file parameters based on uploading profile pictures.

    Thanks.

  3. Code works great; thanks !

    How can we add a URL link in the error message – e.g. “please resize by going here [link to pixlr or similar online app]

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.