Optimizing images is a crucial yet often overlooked aspect of website management. Unfortunately, many users who are not familiar with how websites work may replace images on their website without realizing the images are too large for the web. Â These oversized images not only hinder page load speeds but also impact negatively affect SEO. To prevent this from happening, you can limit the maximum media upload size on the website. In this tutorial I will show you how to limit the maximum media image or file upload size in WordPress for your Divi websites.
Add A PHP Code Snippet To Remove Howdy From The WordPress Admin Bar
The quick and simple way to do this is with the PHP code snippet below. We have instructions in the toggle below for where to paste PHP code if you are not familiar with it. Just remember to be careful, as PHP can cause issues if not implemented properly.
To customize the value, you can change the value of $fileSize according to your needs. In the snippet provided, the default maximum upload file size is set to 512 KB. When this value is multiplied by 1024 kilobytes, it converts to 512 KB * 1024 bytes/KB = 524,288 bytes, which is 0.5 megabytes (MB). Just remember that 1024 KB = 1 MB. So as an example, if you want to set the maximum upload file size to 2 MB, you would provide the file size as ‘2048’ KB.
After adding this snippet to your site, and try to upload an image that is larger than the size specified in the code.
Where To Paste The PHP Code
1. Divi Assistant
If you are using our Divi Assistant plugin, simply paste the code in the PHPÂ tab in the custom code window in the Divi Visual Builder.
2. Child Theme
If you are using a child theme, paste this code into the functions.php file. If you don't have a child theme, you can generate a child theme directly on your site or download our free child theme.
3. Code Snippet Plugins
Otherwise, install a dedicated code snippet plugin, create a new snippet, and paste this code into the PHP code editor.
If you need help understanding where to paste the code, please check out our complete guide about where to add custom PHP code snippets in Divi.
// Function to limit the upload file size
function pac_da_limit_upload_file_size( $size ) {
$fileSize = '512'; // Provide fileSize in KB's, 512 value means 512 KBs
$size = (int)$fileSize * 1024; // Convert KB to bytes
return $size;
}
// Hook the function to the 'upload_size_limit' filter with a priority of 20
add_filter( 'upload_size_limit', 'pac_da_limit_upload_file_size', 20 );
Code Explanation
This code snippet defines a function “pac_da_limit_upload_file_size” that adjusts the maximum upload file size. It takes the current maximum size as an argument, then sets it to a custom value provided in kilibytes. The function is then hooked to the “upload_size_limit” filter with a priority of 20, ensuring it runs at the appropriate time during the WordPress upload process.
How To Limit The Maximum Media Image And File Upload Size In WordPress Using Divi Assistant
Here are the simple steps to limit the maximum media image and file upload size in WordPress using our popular Divi Assistant plugin:
- Install and activate the Divi Assistant plugin
- Click on the Media Helper tab and the Uploads subtab
- Enable the setting
I hope that is easy enough for you! 😉
This is great, thank you but what if I want to allow only webp 800x800px? Can you make snippet for that?
Hi Miljko,
Thanks for that suggestion about file type and file dimensions. Those are more uncommon, but I will keep these in mind to consider.