Auto-Compress WordPress Images on Upload: Setup Guide

WordPress users upload huge images constantly. A 5MB photo becomes 50 pages of your site. The fix: auto-compress every upload at the WordPress layer.

Why WordPress Images Are Too Big

  • Original upload: Full resolution from camera
  • WP sizes: Creates 3-4 thumbnails, but keeps original
  • Editors upload: Often don't resize before upload
  • Plugins: Add image sizes with no optimization

Method 1: WordPress Plugins (No Code)

Smush (Free)

  • Automatic compression on upload
  • Lazy loading built-in
  • Lossless by default
  • 1000 images max per batch

ShortPixel (Free + Paid)

  • Lossy and glossy (better visual) modes
  • Converts to WebP automatically
  • 100 images/month free
  • Best plugin for mixed JPEG/PNG

Imagify (by WP Rocket)

  • Aggressive, Ultra, Normal modes
  • WebP + AVIF output
  • Free tier: 100MB
  • Best for WP Rocket users (same vendor)

Method 2: functions.php Hook (Free, No Plugin)

Add to child theme's functions.php:

add_filter('wp_generate_attachment_metadata', 'auto_compress_upload');
function auto_compress_upload($metadata) {
    $file = $metadata['file'];
    $path = wp_upload_dir()['basedir'] . '/' . $file;
    if (file_exists($path)) {
        $img = imagecreatefromjpeg($path);
        imagejpeg($img, $path, 82);
        imagedestroy($img);
    }
    return $metadata;
}

Method 3: Server-Side with OpenCompressor

Send uploads through our API for advanced compression. Best for high-volume sites.

Method 4: WebP/AVIF Conversion

Convert all images to WebP on save. Requires modern browser support.

  • Converter plugin: ShortPixel, Imagify, or Convert WebP
  • Serve HTML picture tag: Browser picks best format
  • CDN rewrites: Cloudflare, KeyCDN can auto-serve WebP

Which Method for What

SituationBest Choice
Blog with 10 posts/monthSmush free
Photography portfolioShortPixel Glossy
Large e-commerce siteImagify + WebP
No plugins allowedfunctions.php hook

Verify Your Results

After setup:

  1. Upload a test JPEG (aim for 3MB)
  2. Check Media Library — file should be 200-500KB
  3. Inspect image on frontend — should serve WebP if enabled

Best Practice: Pre-Optimize Before Upload

The best plugin is no plugin. Compress images before upload with Batch Compress, then upload already-optimized files.

Benefits:

  • No plugin dependency
  • Faster uploads (smaller files)
  • Less disk usage
  • Better quality control

Conclusion

Use ShortPixel or Imagify if you want set-and-forget. Use pre-compression if you want full control. Never leave WordPress default JPEG (quality 82 but full-size images).

← Back to Blog