î‚£

Subscribe On YouTube

Join other subscribers and enjoy other Divi video tutorials!

How To Limit The Maximum Media Image And File Upload Size In WordPress

Nelson Miller Profile Orange
In this tutorial I will show you how to limit the maximum media image or file upload size in WordPress for your Divi websites.

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 Maximum Media Upload Size using the Divi Assistant plugin

Subscribe For More Things Like This!

At the start of each month, we send out a recap newsletter from the month before with family news, Divi news, our latest tutorials, and product news. Occasionally, if the news is too exciting to wait, we will send out another email separate from the monthly newsletter. That’s what you get when you subscribe, and of course you can unsubscribe if you are no longer interested!

Blog Post Optin
Divi Logo (2)

Shop Our Divi Products

Plugins • Courses • Templates

Visit The Shop

Featured Products

Asset 4

New! Trail Guides

Follow a series of blog posts carefully arranged around a specific topic or goal! Keep track of your progress by marking posts completed, just like a free course!

View Trial Guides

î‚£

Divi Tutorials On YouTube

Our videos have views! Join subscribers and enjoy over video tutorials!

Visit Our Channel

Leave A Response!

By commenting you agree to our Blog & YouTube Comments Policy

2 Comments

Comments By Others

  1. Miljko

    This is great, thank you but what if I want to allow only webp 800x800px? Can you make snippet for that?

    Reply

Submit a Comment

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

Recent Posts

0

Your Cart