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:
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:
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.
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. 😉