Every WordPress site I hand over eventually runs into the same problem. The client starts dragging 15 or 20 MB photos straight off their phone into the media library, nobody resizes anything first, and a month later the homepage is crawling, the uploads folder has ballooned, and the nightly backup takes forever. The fix I reach for before anything else is a hard cap on the media library upload size, so WordPress refuses anything oversized before it ever touches the disk.
Why WordPress lets this happen
Out of the box, WordPress accepts any file up to the ceiling your server allows, set by the PHP upload_max_filesize and post_max_size values. On a modern host that is often 64 MB or more. That headroom is deliberate, but it is the wrong lever for everyday content editing. A current phone camera easily spits out a 12 MB JPEG, and once that full-resolution file is in the library it gets stored, resized, served, and backed up forever. The media uploader has no per-file cap in the admin UI, so left to their own devices people upload whatever they happen to have.
The fix: cap the upload size with one filter
WordPress exposes a filter called upload_size_limit that controls the maximum size the media uploader will accept. I hook into it and return my own ceiling in bytes. Here is the snippet I use, defaulted to 512 KB:
// 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 );
The value is in kilobytes. 512 KB is aggressive on purpose, because it forces properly optimised web images, which is exactly what you want on a fast site. If that is too tight for a particular client, bump $fileSize to 2048 for a 2 MB cap or 5120 for 5 MB. The priority of 20 just makes sure this runs after most other plugins that might touch the same filter.
The nice part is that this is not a silent block. When someone tries to add an oversized file, the media library shows a plain file size error telling them the maximum allowed, so the client understands what happened and resizes the image instead of emailing me to ask why the upload failed. I usually pair the cap with an image optimisation plugin so the files that do get through are compressed on the way in, but the hard limit is the part that actually protects the site from a 30 MB drag-and-drop.
How to install it
- Install and activate the free Code Snippets plugin.
- Go to Snippets, Add New, and paste the code above.
- Set it to run everywhere, then save and activate.
- Try uploading an image larger than your limit. WordPress will reject it with a file size error.
I never touch functions.php for this kind of thing. Code Snippets keeps the logic separate from the theme, so it survives theme updates and switches without me losing anything.
Still works in 2026
The upload_size_limit filter is core WordPress and has not changed. I am running this exact snippet on live sites in 2026 with no deprecation warnings, and it sits happily alongside LiteSpeed Cache and any image optimisation plugin, because it stops the oversized file at the door rather than cleaning up after it.
The snippet lives on GitHub: djaysan/limit-media-upload-size. Star it or fork it if it saves you some time.
Need a hand tightening up a slow WordPress or Elementor build? Get in touch.