This is part of DevKit but I would like to share code snippet as well
<?php
/*
Plugin Name: Attachments Redirect Local Images to Live
Description: Redirects local image URLs to the live site.
Version: 1.0
Author: Marko Krstic
*/
add_filter('wp_get_attachment_url', 'replace_attachment_url');
add_filter('wp_calculate_image_srcset', 'replace_srcset_urls', 10, 5);
add_filter('the_content', 'replace_content_urls');
function replace_attachment_url($url)
{
return replace_local_with_live_url($url);
}
function replace_local_with_live_url($url)
{
// Fetch local URL dynamically
$local_url = trailingslashit(get_home_url());
$live_url = 'https://dplugins.com/';
// Replace the local URL with the live URL
$new_url = str_replace($local_url, $live_url, $url);
return $new_url;
}
function replace_srcset_urls($sources, $size_array, $image_src, $image_meta, $attachment_id)
{
foreach ($sources as $key => $source) {
$sources[$key]['url'] = replace_local_with_live_url($source['url']);
}
return $sources;
}
function replace_content_urls($content)
{
// Use the DOMDocument class to parse the content
$doc = new DOMDocument();
@$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
// Replace img src attributes
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
$src = $image->getAttribute('src');
$image->setAttribute('src', replace_local_with_live_url($src));
}
// Replace anchor href attributes
$anchors = $doc->getElementsByTagName('a');
foreach ($anchors as $anchor) {
$href = $anchor->getAttribute('href');
$anchor->setAttribute('href', replace_local_with_live_url($href));
}
// Save the changes and return the modified content
$content = $doc->saveHTML();
return $content;
}
What This WordPress Plugin Does
This plugin is designed for people who work on a WordPress site locally (for development or testing) but want all the images and media files to load from the live production site instead of the local machine. That way, you don’t need to copy the whole uploads folder to your local environment just to see images.
How It Works
- Plugin header The block at the top (Plugin Name, Description, Version, Author) is just WordPress metadata so the plugin shows up correctly in the admin panel.
- Filters hooked in
- wp_get_attachment_url → Changes the URL whenever WordPress tries to fetch an image attachment.
- wp_calculate_image_srcset → Updates all srcset image URLs (those responsive image variations).
- the_content → Parses post content and replaces image src and link href attributes.
- replace_local_with_live_url() This is the core function. It figures out what your local site’s base URL is (get_home_url()) and replaces it with the live site URL (https://dplugins.com/). So if your local URL is something like:
http://localhost:8888/wp-content/uploads/2023/10/image.jpg
It automatically becomes:
https://dplugins.com/wp-content/uploads/2023/10/image.jpg
- Attachment URLs Whenever WordPress looks up an attachment, the plugin swaps out the base domain for the live one.
- Srcset URLs All alternative image sizes in the srcset attribute are looped through and replaced too, so responsive images still point to the live server.
- Post content This part uses DOMDocument to scan through the HTML of your post. It updates:
- <img src=”…”> attributes
- <a href=”…”> attributes
Why It’s Useful
- Faster local development → You don’t need to sync gigabytes of media files.
- Consistency → Images always load from the live site, so the content looks exactly the same locally.
- Automatic → You don’t need to manually replace links; WordPress does it on the fly.
👉 So in short: this plugin automatically redirects all local image and media URLs to your live site’s domain, covering attachments, responsive srcset, and even post content.