Wordpress Development

Limiting Media Upload File Sizes in WordPress

- Updated: August 12th, 2021 -

I’ve recently come across an issue with a very old WordPress function I wrote a few years ago. The basic idea was to check if the user was logged in, see what kind of user it was and limit the file size of uploaded materials. After running into a bug recently, I went online to find a newer version of the function and was surprised at how little there was for documenting this simple addition. There’s a lot of articles explaining how to increase file size but not as many that showed how to limit file sizes. In my opinion, this is a necessity when handing off web site editing capabilities to clients. So, my original function needed a refresh so I rebuilt it and created a very simple plugin, and it works well.

The idea is quite simple; verify if the logged in user is an Admin or not and adjust the limit for the native WordPress media uploader to the limits specified within the plugin. By default, it sets the max upload size to 1MB for site admins and 750KB for all other users. This can be tweaked of course, but it’s a decent starting point. Although even 1MB is far too large for image files, it can be adjusted accordingly.

You can download the plugin here and tweak to your own needs. It’s a super simple approach to a problem that happens all too often when passing your well developed site to managers and editors that do not consider image optimization techniques.

For those that want to understand how to make this yourself, let’s get to it below:

Create a WordPress function to limit file size uploads

Alright, for this quick tip we’ll utilize some resources here as background:


The following links show how to handle working with certain capabilities within a user’s role. Note that it’s better practice to define limitations based on capabilities instead of roles.

What we’re basically after is a simple way to see what kind of capabilities a user has so we can better determine different file size restrictions. For example, if you are the site administrator, there’s probably a good chance that you will be uploading something that has already been optimized so that the file size will make the internet a happier place. If, however, the user is a page editor, the chances increase that the image size may be coming straight from a hi-res photo.

Here’s a block of code that can be added to your theme’s functions.php file, I recommend using a child theme instead of the parent theme to avoid losing any changes you make due to an upgrade.

function file_size_upload_limit_filter( $size ) {

   if( current_user_can( 'manage_options' ) ) {
      // Set limit to 1MB (1024KB) for Admins
      // Editing this number allows for Admin adjustments.
      $limit = 1000;

   } else {
      // Set limit to 750KB for non-admins
      // Editing this number allows for non-Admin adjustments.
      $limit = 750;
   $size = 1024 * $limit;
   return $size;
add_filter( 'upload_size_limit', 'file_size_upload_limit_filter', 20 );

Cool, now let’s do a quick walk through on this block of code. What we start with is a standard PHP function that takes $size as an argument.

Next we test to see if the user is an admin or not by using the current_user_can() function and set a value of 1MB for admins. If the user is not an admin, then we default to setting the 750KB file size.

Finally, after declaring the $limit variable, we return the $size argument. Afterwards you can test it out by lowering the allowed limit size and trying to upload an image that is higher than your allotted size limit.

When you try to upload a document or image with a file size that is larger than your declared maximum size, your upload fails and gives an error message that it exceeds the upload size. Handy right?

Leave a Reply

Your email address will not be published. Required fields are marked *