🧾 Step 1: Full Updated Code
<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'download-images', 'Download_External_Images_Command' );
}
class Download_External_Images_Command {
public function __invoke( $args, $assoc_args ) {
$posts = get_posts([
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => ['publish', 'draft'], // ✅ Published aur Draft dono ko include kiya
]);
foreach ( $posts as $post ) {
$updated_content = $this->process_images( $post->post_content, $post->ID );
if ( $updated_content !== $post->post_content ) {
wp_update_post([
'ID' => $post->ID,
'post_content' => $updated_content,
]);
WP_CLI::success( "✅ Updated post {$post->ID}" );
}
}
}
private function process_images( $content, $post_id ) {
preg_match_all( '/<img[^>]+src=[\'"]([^\'"]+)[\'"]/i', $content, $matches );
if ( empty( $matches[1] ) ) return $content;
foreach ( $matches[1] as $img_url ) {
// Agar image already local hai to skip karo
if ( strpos( $img_url, home_url() ) !== false ) continue;
// External image download karna
$new_img = media_sideload_image( $img_url, $post_id, null, 'src' );
if ( ! is_wp_error( $new_img ) ) {
$content = str_replace( $img_url, $new_img, $content );
}
}
return $content;
}
}
🛠 Step 2: File Edit Karna
- SSH terminal me ho jao
public_html
folder me:cd ~/domains/theworldflowers.com/public_html
download-images.php
ko edit karo:nano download-images.php
- Pura purana code delete karo aur upar wala updated code paste karo.
- Save & exit:
- Ctrl + O → Enter
- Ctrl + X
▶️ Step 3: Command Run Karna
wp --require=download-images.php download-images
Output ka example:
✅ Updated post 8
✅ Updated post 12
✅ Updated post 14
📋 Confirm Karne Ke Steps
- WordPress Admin Panel kholo:
https://theworldflowers.com/wp-admin/
- Check karo:
- Media → Library me naye images aaye?
- Posts → Drafts me jaake dekho ki image ka
src
ab local hai
🎁 Bonus Features (Optional)
- Featured image auto-set karna
- Sirf wahi post scan karna jisme external images ho
- Batches me process karna