wp-cli/media-command
WP-CLI media commands for WordPress: import files as attachments, regenerate thumbnails, replace attachment files, list registered image sizes, and fix image orientation. Useful for bulk media maintenance and automation from the command line.
Installation:
composer require wp-cli/media-command
Or via WP-CLI:
wp package install wp-cli/media-command
First Use Case: List all registered image sizes to understand your WordPress environment:
wp media image-size
Quick Import: Import a local image and attach it to a post:
wp media import ~/path/to/image.jpg --post_id=123 --title="My Image"
wp media regenerate --yes
wp media prune --remove-abandoned --yes
wp media import ~/image.jpg --post_id=456 --featured_image
wp media replace 789 ~/new-image.jpg
wp media import http://example.com/image1.jpg http://example.com/image2.jpg --post_id=789
ATTACHMENT_ID=$(wp media import ~/image.jpg --porcelain)
wp post meta add 789 _thumbnail_id $ATTACHMENT_ID
wp media fix-orientation --dry-run # Preview
wp media fix-orientation # Apply
wp media image-size
Run WP-CLI from Laravel:
Use Artisan to execute WP-CLI commands via shell:
// app/Console/Commands/RegenerateThumbnails.php
public function handle() {
$exitCode = Artisan::call('wp media regenerate --yes');
if ($exitCode !== 0) {
$this->error('Thumbnail regeneration failed.');
}
}
Dynamic Post IDs:
Fetch post IDs from Laravel and pass them to wp media import:
$posts = Post::where('status', 'publish')->get();
foreach ($posts as $post) {
Artisan::call('wp media import', [
'path/to/image.jpg',
'--post_id=' . $post->id,
'--title=' . $post->title,
]);
}
Event-Driven Workflows:
Trigger WP-CLI commands after Laravel events (e.g., postUpdated):
event(new PostUpdated($post));
// In listener:
Artisan::queue('wp media regenerate ' . $post->id);
Bash Script for Bulk Imports:
#!/bin/bash
find ~/uploads -name "*.jpg" | while read file; do
wp media import "$file" --post_id=123 --skip-duplicates
done
Laravel Task Scheduler: Schedule thumbnail regeneration nightly:
$schedule->command('wp media regenerate --yes')->daily();
Permission Issues:
www-data) has write access to:
wp-content/uploads/wp-content/uploads/2023/05/).sudo or adjust permissions:
chown -R www-data:www-data wp-content/uploads/
Memory Limits:
memory_limit in wp-cli.yml or php.ini:
memory_limit = 512M
Duplicate Handling:
--skip-duplicates may not work as expected if filenames differ but content is identical.wp media import --porcelain + custom logic to check hashes.URL vs. Local Paths:
--skip-copy (files are downloaded temporarily).cd into the correct directory first.Multisite Conflicts:
--url.wp --url=https://site.com media regenerate
Dry Runs:
--dry-run or --yes to test commands safely:
wp media fix-orientation --dry-run
wp media prune --yes
Verbose Output:
WP_CLI_DEBUG=1 wp media import ~/image.jpg
Log File Analysis:
wp-content/debug.log for errors after failed operations.Image Size Mismatches:
wp media image-size
functions.php:
add_image_size('custom-size', 800, 600);
Custom Image Sizes:
add_filter('intermediate_image_sizes_advanced', function($sizes) {
$sizes['my-custom-size'] = ['width' => 1200, 'height' => 800, 'crop' => true];
return $sizes;
});
Pre/Post-Import Hooks:
add_action('add_attachment', function($attachment_id) {
// Custom logic after import
});
WP-CLI Aliases:
~/.wp-cli/config.yml:
aliases:
regen-thumbs: 'media regenerate --yes'
clean-thumbs: 'media prune --remove-abandoned --yes'
Testing:
Given that HTTP requests to "http://example.com" will respond with:
"""
<binary-image-data>
"""
Batch Processing:
xargs for large datasets:
seq 1 1000 | xargs -P 4 wp media regenerate
Post Meta Updates:
_wp_attachment_metadata after replacements:
wp media replace 123 ~/new.jpg --skip-delete
wp post meta update 123 _wp_attachment_metadata '{"width": 2000, "height": 1500}'
CI/CD Integration:
# After theme/plugin updates
wp media regenerate --yes
Performance:
parallel -j 4 wp media regenerate ::: $(seq 1 1000)
How can I help you explore Laravel packages today?