• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Set a size limit to the files that can be uploaded to WordPress

Discover in this tutorial how to set a limit to the size of the files that users can upload to the WordPress media library using code.

If you manage a website where users can upload files, you may have encountered the problem that the hosting space runs out very quickly because users upload very large files.

One way to limit this problem is to put a weight limit on the files that can be uploaded.

Let’s see how to do it.

Snippet to limit the size of the files that can be uploaded to WordPress media library

Add the following code at the end of functions.php or in your functionality plugin:

To see this and another 1097 code snippets of this website, login or subscribe here.

In this code you use the upload_size_limit filter to disallow uploading images larger than 2MB.

Obviously, you can adjust this size to whatever size you consider appropriate.

You can also customize it to affect different user roles. For example, if you do not want this restriction to affect users with administrator role you can use this modification:

To see this and another 1097 code snippets of this website, login or subscribe here.

In this case, you use the upload_size_limit filter in conjunction with the current_user_can function to run only if the user is not an administrator.

Conclusion

Now you know how to limit the size of files that users can upload to WordPress. You can also make this limitation only affect a certain user role.

Any questions? Let me know in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

PHP WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2026

close-icon

Lost your password?