Automatically Downsize Large Images in WordPress Using Hooks & WebP

Uploading big images to your WordPress site? š Slows it down, right? Nobody likes a slow siteānot you, not your visitors, and certainly not Google. But donāt stress! Thereās a smart way to automatically downsize large images in WordPress using hooks and convert them to WebPāthe webās favorite lightweight image format.
In this article, weāll walk you through it all. No developer degree required. Just some simple tweaks and fun coding magic. Letās roll! š
šÆ Why Optimize Images at All?
Glad you asked! Hereās why optimizing images is a big deal:
- Faster page loads = happier visitors.
- Better SEO – Google loves speed.
- Less storage and bandwidth usage.
- Mobile-friendliness – perfect for smaller screens.
Now imagine doing this automaticallyāevery time you upload an image. Thatās the dream. Letās make it real.
š§© What Are Hooks in WordPress?
Hooks are awesome. Theyāre like secret entrances into WordPress code. Using hooks, you can tell WordPress to do something cool every time an event happens. For example, when someone uploads a new image.
There are two types:
- Action hooks ā Do something when a certain event happens.
- Filter hooks ā Change data before it gets used or displayed.
To downsize images, weāll use an action hook triggered when media uploads, and then filter the image output to convert it into a WebP.
š” How to Automatically Downsize Images
Here’s a step-by-step guide to resize images as soon as they are uploaded. No effort, all magic!
-
Open your theme’s
functions.php
fileYou can find it under Appearance > Theme File Editor. But… always make a backup first!
-
Add this code snippet:
add_filter('wp_handle_upload_prefilter', 'limit_image_dimensions'); function limit_image_dimensions($file) { $imageEditor = wp_get_image_editor($file['tmp_name']); if (!is_wp_error($imageEditor)) { $size = $imageEditor->get_size(); if ($size['width'] > 1920 || $size['height'] > 1080) { $imageEditor->resize(1920, 1080, false); $imageEditor->save($file['tmp_name']); } } return $file; }
This code resizes any giant image to max dimensions of 1920×1080. Feel free to adjust!
Once in place, this will make your uploads lighter and faster without you lifting a finger.

š¼ļø What About WebP Format?
Glad you remembered. WebP is like JPEGās smarter cousin. It keeps your image looking sharp but makes the file size way smaller.
WordPress already supports WebP natively from version 5.8 and up. You just need to tell it to convert your images to WebP when theyāre uploaded. Letās do it.
š Convert JPEG/PNG to WebP on Upload
Weāll use a filter hook and a bit of PHP. Donāt worryāitās painless.
-
Save your original image (optional)
If you want to keep the original JPEG or PNG too, add this before the conversion line:
// This step is optional: copy($file['tmp_name'], $upload_dir['path'] . '/' . $filename . '-original.' . $ext);
-
Add this full snippet to
functions.php
:add_filter('wp_handle_upload', 'convert_image_to_webp'); function convert_image_to_webp($upload){ $file = $upload['file']; $info = pathinfo($file); $ext = strtolower($info['extension']); if (in_array($ext, ['jpg', 'jpeg', 'png'])) { $img = imagecreatefromstring(file_get_contents($file)); $webpPath = $info['dirname'] . '/' . $info['filename'] . '.webp'; imagewebp($img, $webpPath, 80); imagedestroy($img); unlink($file); // Delete original file $upload['file'] = $webpPath; $upload['url'] = str_replace($info['basename'], $info['filename'] . '.webp', $upload['url']); $upload['type'] = 'image/webp'; } return $upload; }
Thatās it! When you upload a JPEG or PNG, WordPress now replaces it with a WebP version. šØ
š§ Important Notes
- Make sure your server has
imagewebp()
support (via GD or Imagick). - Always test on a staging site before going live.
- Some older browsers donāt fully support WebP. Consider fallbacks if needed.
Want to check if WebP works? Right-click an image > Open in new tab > Check the file extension!”

š”ļø Using a Plugin Instead (For the Lazy Days)
Not feeling like messing with code? No problem. There are great plugins that can do this for you. Some favorites:
- ShortPixel ā Converts images to WebP and resizes.
- Smush ā Super popular, free versions available.
- WebP Converter for Media ā Simple and focused on just that.
Plugins are great. But doing it yourself with hooks gives you full control and doesnāt add any plugin bloat to your site.
š„ Final Tip: Make It Foolproof
Want to go the extra mile? Add a notice if users upload super large images like 5000x5000px. This helps non-tech users know whatās happening behind the scenes.
add_filter('wp_handle_upload_prefilter', 'warn_large_upload');
function warn_large_upload($file){
list($width, $height) = getimagesize($file['tmp_name']);
if ($width > 4000 || $height > 4000){
echo 'Hey! Your image is huge. Weāre resizing it for performance. š';
}
return $file;
}
š Wrapping It All Up
You did it! š You learned:
- Why large images are bad for WordPress performance.
- How to use hooks to automatically resize them.
- How to convert them to WebP on the fly.
- How to choose between code and plugins.
Your site is now faster, smarter, and more user-friendly. All thanks to a little PHP and a lot of caffeine (probably ā).
Now go out there and build something amazing. Or, just enjoy that your site is loading like a rocket. Either works. š
Happy WordPressing! š»š