Discover in this tutorial how to set a minimum resolution limit to the images that users can upload to the WordPress media library, using code.
If you manage a website where other users can upload files you may have encountered the problem that sometimes they upload images so small that they are not displayed correctly.
One way to limit this problem is to require a minimum resolution in the images they can upload.
Let’s see how to achieve this.
Snippet to make the images that can be uploaded to the WordPress media library have minimum dimensions
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 wp_handle_upload_prefilter
filter to only allow uploading images with a minimum of 300 pixels in height and width.
Obviously, you can adjust this size to whatever size you think appropriate and change the message to the one you want to be displayed to users.
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 wp_handle_upload_prefilter
filter together with the current_user_can
function so that it is applied only if the user is not an administrator.
Conclusion
Now you know how to make sure that the images that users upload to WordPress have the minimum dimensions to be displayed correctly. You can also make this limitation only affect a certain user role.
Any questions? I read you 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. 😉