WP-CLI Commands

Manage image optimization from the command line

Compresso registers a wp compresso command with three subcommands for optimizing, restoring, and checking status from the terminal.

wp compresso optimize

Optimize images in the media library.

wp compresso optimize [--all] [--id=<ids>] [--type=<type>] [--since=<date>] [--limit=<number>] [--skip-thumbnails] [--force] [--dry-run]

You must specify either --all or --id.

Options

OptionDescription
--allOptimize all unoptimized images
--id=<ids>Comma-separated attachment IDs to optimize
--type=<type>Filter by format: jpeg, png, gif, or webp
--since=<date>Only images uploaded after this date. Accepts relative dates ("30 days") or absolute ("2024-01-01")
--limit=<number>Maximum number of images to process
--skip-thumbnailsOnly optimize the main image, skip thumbnail sizes
--forceRe-optimize images that have already been optimized
--dry-runPreview what would be optimized without processing

Examples

# Optimize all unoptimized images
wp compresso optimize --all

# Optimize specific images by ID
wp compresso optimize --id=123,456,789

# Optimize only PNGs uploaded in the last month
wp compresso optimize --all --type=png --since="30 days"

# Process at most 100 images
wp compresso optimize --all --limit=100

# Re-optimize everything with current settings, skip thumbnails
wp compresso optimize --all --force --skip-thumbnails

# Preview what would be optimized
wp compresso optimize --all --dry-run

Output

Found 5 image(s) to optimize.
Optimizing images [============================] 5/5
  ✓ #123: photo.jpg - saved 245 KB (35.2%)
  ✓ #456: banner.png - saved 156 KB (22.1%)
  ✓ #789: icon.gif - saved 89 KB (18.5%)
  ✓ #101: hero.jpg - saved 312 KB (41.7%)
  ✓ #102: logo.png - saved 178 KB (28.3%)

Success: Optimization complete. Processed: 5, Success: 5, Failed: 0, Total savings: 980 KB

wp compresso status

Show optimization statistics for the entire library or a specific image.

wp compresso status [--id=<id>] [--format=<format>]

Options

OptionDescription
--id=<id>Show detailed status for a specific attachment. If omitted, shows overall stats.
--format=<format>Output format: table (default), json, or yaml

Examples

# Overall optimization stats
wp compresso status

# Detailed info for a specific image
wp compresso status --id=123

# Output as JSON (useful for scripting)
wp compresso status --format=json

Overall Status Output

Compresso Status
────────────────────────────────────
  Total Images:    1,250
  Optimized:       1,089
  Pending:         161
  Total Savings:   2.5 GB
  WebP Created:    987
  Optimization %:  87.1%

Settings
────────────────────────────────────
  JPEG Quality:    82
  PNG Quality:     80
  WebP Quality:    80
  Auto Optimize:   Yes
  Auto WebP:       Yes

Image-Specific Output

Image Status: #123
──────────────────────────────────────────────
  ID:               123
  Filename:         photo.jpg
  MIME Type:        image/jpeg
  Status:           Optimized
  Current Size:     456 KB
  Original Size:    789 KB
  Savings:          333 KB
  Savings %:        42.2%
  Tool Used:        jpegoptim
  WebP Available:   Yes (234 KB)
  Has Backup:       Yes
  Dimensions:       3840x2560
  Optimized At:     2024-01-15 14:30:22

Thumbnails
─────────────────────────────────────���────────
  thumbnail:        saved 12 KB
  medium:           saved 28 KB
  large:            saved 67 KB

JSON Output

wp compresso status --format=json
{
  "total_images": 1250,
  "optimized_images": 1089,
  "pending_images": 161,
  "total_savings": 2684354560,
  "total_savings_human": "2.5 GB",
  "webp_created": 987,
  "optimization_percent": 87.1
}

wp compresso restore

Restore optimized images to their original state from local backups.

wp compresso restore [--id=<ids>] [--all] [--dry-run]

You must specify either --all or --id.

Options

OptionDescription
--id=<ids>Comma-separated attachment IDs to restore
--allRestore all optimized images that have backups
--dry-runPreview what would be restored without processing

Using --all without --dry-run prompts for confirmation before proceeding.

What Restore Does

For each image restored:

  1. Copies the original file back from the backup directory
  2. Removes all Compresso optimization metadata
  3. Deletes any generated WebP and AVIF files (main image and thumbnails)
  4. Regenerates WordPress thumbnails from the restored original
  5. Updates global stats

Examples

# Restore a single image
wp compresso restore --id=123

# Restore multiple images
wp compresso restore --id=123,456,789

# Restore all images (prompts for confirmation)
wp compresso restore --all

# Preview what would be restored
wp compresso restore --all --dry-run

Output

# Dry run
wp compresso restore --all --dry-run
Dry run - the following images would be restored:
  #123: photo.jpg
  #456: banner.png
  #789: icon.gif

Success: Would restore 3 image(s). Remove --dry-run to execute.
# Actual restore
wp compresso restore --id=123,456,789
Restoring images [============================] 3/3
  ✓ #123: photo.jpg - restored
  ✓ #456: banner.png - restored
  ✓ #789: icon.gif - restored

Success: Restore complete. Processed: 3, Success: 3, Failed: 0

Only images with existing backups in wp-content/uploads/compresso-backups/ can be restored. If an image's backup file is missing, it will be skipped.

On this page