WordPress: Auto-Import External Images to Media Library (Block Editor Compatible)
If you've ever pasted content from Google Docs, copied images from other websites, or imported posts that reference external image URLs, you know the pain: WordPress keeps those images as remote links. They're not in your media library. They break when the source site removes them. And WordPress doesn't even recognize them as “real” images. I built this plugin to solve exactly that problem. Every time you save a post, it automatically downloads all external images, uploads them to your media library, and updates your content to use the local versions. No manual intervention is needed. What This Plugin Does When you save any post or page (draft, published, or whatever), this plugin: Scans your content for any external image URLs Downloads each image to your server Creates a proper media library entry with all the metadata Replaces the entire image tag with WordPress-native markup Adds responsive image sizes (srcset, sizes attributes) Includes the wp-image-{ID} class so WordPress knows it's local It works with both the classic editor and the block editor (Gutenberg). It even handles Google Docs images and other redirected URLs. Key Features Smart Image Naming Images are named based on your post title, not random URLs. If your post is “ChatGPT vs Claude vs Gemini for Product Strategy,” the images become: chatgpt-vs-claude-vs-gemini-for-product-strategy.jpg chatgpt-vs-claude-vs-gemini-for-product-strategy-2.jpg chatgpt-vs-claude-vs-gemini-for-product-strategy-3.jpg Optional Metadata Auto-Fill You can configure whether the plugin auto-fills: Image title Alt text Caption Description By default, all are set to false so you control this yourself. Performance Safeguards Maximum 50 images per save (configurable) Content size limit of 5 MB to prevent timeouts Deduplication - won't re-download images that already exist Stores a map of processed URLs to avoid redundant downloads Block Editor Compatible This version (2.0.0) is a complete rewrite that properly integrates with Gutenberg. The images now include: Proper wp-image-{ID} classes Responsive image sizes (thumbnail, medium, large, full) Srcset and sizes attributes for responsive loading Integration with WordPress's native image handling Configuration At the top of the plugin file, you'll find these settings: php define ( 'RR_EXTIMG_SET_TITLE' , false ) ; define ( 'RR_EXTIMG_SET_ALT' , false ) ; define ( 'RR_EXTIMG_SET_CAPTION' , false ) ; define ( 'RR_EXTIMG_SET_DESCRIPTION' , false ) ; define ( 'RR_EXTIMG_DEFAULT_SIZE' , 'large' ) ; Set any to true to auto-fill that metadata field. The RR_EXTIMG_DEFAULT_SIZE controls which image size gets used in your content (options: thumbnail , medium , large , full ). How It Works (Technical Overview) The plugin hooks into WordPress's save process at multiple points: save_post hook - Catches manual saves in the admin rest_after_insert_post hook - Catches block editor saves via REST API rest_after_insert_page hook - Catches page saves via REST API When triggered, it: Extracts Images: Parses HTML for <img> tags Also parses Gutenberg block JSON for image URLs Captures the full tag, not just the src Validates & Downloads: Checks if URL is external (not your own domain) Validates image extension (jpg, png, gif, webp, svg, ico, bmp) Handles Google Docs/Drive redirects Downloads to temp file using WordPress's download_url() Creates Media Entry: Uses media_handle_sideload() for proper WordPress integration Generates all responsive image sizes Stores original URL as post meta for reference Optionally fills in title, alt, caption, and description Replaces Content: Generates proper WordPress image HTML with all attributes Includes wp-image-{ID} class Adds srcset and sizes for responsive images Replaces the entire <img> tag in your content Updates the post without triggering another save loop Stores Mapping: Monitors which URLs have been processed Prevents re-downloading on subsequent saves Stored as post meta: _rr_extimg_map Use Cases I use this for: Content imports from other sites or Google Docs Guest posts where contributors paste content with external images Legacy content migration to ensure all images are local SEO to control image file names and metadata Site reliability - no more broken images when external sites go down Installation Method 1: Manual Installation Copy the plugin code into a new file: wp-content/plugins/rr-import-external-images/rr-import-external-images.php Activate the plugin: Go to WordPress Admin → Plugins Find “RR — Import External Images on Save” Click Activate Method 2: Use a Code Snippet plugin like WPCodeBox or FluentSnippets. Copy the snippet below and paste it in your favorite code snippet plugin.
WPCodeBox