A WordPress utility script that automatically converts full-resolution images in Gutenberg blocks to optimized sizes (medium or large).
This script scans all posts in your WordPress site and finds Gutenberg image blocks that are using full-resolution images. It then converts them to use a specified size (medium or large), updating both the image URL and block attributes.
- Performance optimization: Full-resolution images can significantly slow down page load times
- Bandwidth reduction: Serving appropriately-sized images reduces data transfer
- After bulk imports: When content has been imported with full-size images
- Site audit cleanup: When you discover posts are serving unnecessarily large images
- Before CDN migration: Clean up image sizes before implementing a CDN strategy
- WordPress installation with
wp-load.phpaccessible - PHP CLI access
- Gutenberg/Block editor content (won't affect Classic Editor content)
- Download the script to your WordPress root directory (where
wp-load.phpis located) - Ensure it's executable:
chmod +x resize-post-images.php
Always run in dry-run mode first to see what will be changed:
# Preview with large size (default)
php resize-post-images.php
# Preview with medium size
php resize-post-images.php --size=mediumOnce you've reviewed the dry-run output:
# Execute with large size
php resize-post-images.php --execute
# Execute with medium size
php resize-post-images.php --size=medium --execute--size=mediumor--size=large- Target image size (default: large)--execute- Apply changes (without this flag, runs in dry-run mode)
The script modifies Gutenberg image blocks by:
- Adding or updating the
sizeSlugattribute to the target size - Updating the
<figure>class to includesize-{target} - Replacing the image
srcURL with the target size URL
<!-- wp:image {"id":123} -->
<figure class="wp-block-image">
<img src="full-size-image.jpg" />
</figure>
<!-- /wp:image --><!-- wp:image {"id":123,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large">
<img src="large-size-image.jpg" />
</figure>
<!-- /wp:image -->- Dry-run by default: Won't make changes unless
--executeis specified - Smart detection: Only processes images that are currently set to full size or have no size designation
- Size validation: Skips images where the target size doesn't exist or is the same as full size
- Preserves small images: If an image is smaller than the target size threshold, it's left unchanged
The script provides detailed output including:
- Total posts processed
- Posts modified
- Images resized per post
- Before/after dimensions for each image
- Summary statistics
Always backup your database before running with --execute:
# Using WP-CLI
wp db export backup-before-resize.sql
# Or use your hosting provider's backup toolsWordPress default sizes:
- Thumbnail: 150x150px (cropped)
- Medium: 768px max width/height
- Large: 1024px max width/height
- Full: Original uploaded size
These dimensions can be customized in Settings → Media.
- Only works with Gutenberg/Block editor content
- Requires the target image size to exist (WordPress generates these on upload)
- Won't regenerate thumbnails if they're missing (use a plugin like Regenerate Thumbnails first)
"Target size doesn't exist": Run a thumbnail regeneration plugin to create missing image sizes.
"No changes detected": Your images might already be using appropriate sizes, or they're smaller than the target size threshold.
Memory issues with large sites: Modify the script to process posts in batches by adjusting the posts_per_page parameter.
MIT License - Feel free to use and modify as needed.
Found a bug or have a suggestion? Open an issue or submit a pull request.