{"id":46406,"date":"2025-08-16T08:40:56","date_gmt":"2025-08-16T08:40:56","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46406"},"modified":"2025-08-16T08:40:56","modified_gmt":"2025-08-16T08:40:56","slug":"attachments-redirect","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/attachments-redirect\/","title":{"rendered":"Attachments redirect"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">This is part of <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/devkit\/\" target=\"_blank\" rel=\"noopener\" title=\"DevKit\">DevKit<\/a> but I would like to share code snippet as well<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">&lt;?php\n\/*\nPlugin Name: Attachments Redirect Local Images to Live\nDescription: Redirects local image URLs to the live site.\nVersion: 1.0\nAuthor: Marko Krstic\n*\/\n\n\nadd_filter('wp_get_attachment_url', 'replace_attachment_url');\nadd_filter('wp_calculate_image_srcset', 'replace_srcset_urls', 10, 5);\nadd_filter('the_content', 'replace_content_urls');\n\nfunction replace_attachment_url($url)\n{\n    return replace_local_with_live_url($url);\n}\n\nfunction replace_local_with_live_url($url)\n{\n    \/\/ Fetch local URL dynamically\n    $local_url = trailingslashit(get_home_url());\n    $live_url = 'https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/';\n\n    \/\/ Replace the local URL with the live URL\n    $new_url = str_replace($local_url, $live_url, $url);\n\n    return $new_url;\n}\n\nfunction replace_srcset_urls($sources, $size_array, $image_src, $image_meta, $attachment_id)\n{\n    foreach ($sources as $key =&gt; $source) {\n        $sources[$key]['url'] = replace_local_with_live_url($source['url']);\n    }\n\n    return $sources;\n}\n\nfunction replace_content_urls($content)\n{\n    \/\/ Use the DOMDocument class to parse the content\n    $doc = new DOMDocument();\n    @$doc-&gt;loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));\n\n    \/\/ Replace img src attributes\n    $images = $doc-&gt;getElementsByTagName('img');\n    foreach ($images as $image) {\n        $src = $image-&gt;getAttribute('src');\n        $image-&gt;setAttribute('src', replace_local_with_live_url($src));\n    }\n\n    \/\/ Replace anchor href attributes\n    $anchors = $doc-&gt;getElementsByTagName('a');\n    foreach ($anchors as $anchor) {\n        $href = $anchor-&gt;getAttribute('href');\n        $anchor-&gt;setAttribute('href', replace_local_with_live_url($href));\n    }\n\n    \/\/ Save the changes and return the modified content\n    $content = $doc-&gt;saveHTML();\n    return $content;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What This WordPress Plugin Does<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This plugin is designed for people who work on a <strong>WordPress site locally<\/strong> (for development or testing) but want all the <strong>images and media files to load from the live production site<\/strong> instead of the local machine. That way, you don\u2019t need to copy the whole uploads folder to your local environment just to see images.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>How It Works<\/strong><\/h3>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Plugin header<\/strong> The block at the top (Plugin Name, Description, Version, Author) is just WordPress metadata so the plugin shows up correctly in the admin panel.<\/li>\n\n\n\n<li><strong>Filters hooked in<\/strong>\n<ul class=\"wp-block-list\">\n<li>wp_get_attachment_url \u2192 Changes the URL whenever WordPress tries to fetch an image attachment.<\/li>\n\n\n\n<li>wp_calculate_image_srcset \u2192 Updates all srcset image URLs (those responsive image variations).<\/li>\n\n\n\n<li>the_content \u2192 Parses post content and replaces image src and link href attributes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>replace_local_with_live_url()<\/strong> This is the core function. It figures out what your local site\u2019s base URL is (get_home_url()) and replaces it with the live site URL (https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/). So if your local URL is something like:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/http\/localhost:8888\/wp-content\/uploads\/2023\/10\/image.jpg<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><br>It automatically becomes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/wp-content\/uploads\/2023\/10\/image.jpg<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Attachment URLs<\/strong> Whenever WordPress looks up an attachment, the plugin swaps out the base domain for the live one.<\/li>\n\n\n\n<li><strong>Srcset URLs<\/strong> All alternative image sizes in the srcset attribute are looped through and replaced too, so responsive images still point to the live server.<\/li>\n\n\n\n<li><strong>Post content<\/strong> This part uses DOMDocument to scan through the HTML of your post. It updates:<ul><li>&lt;img src=&#8221;&#8230;&#8221;> attributes<\/li><li>&lt;a href=&#8221;&#8230;&#8221;> attributes<\/li><\/ul>That way, even hardcoded images or links in post content will point to the live site.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why It\u2019s Useful<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Faster local development<\/strong> \u2192 You don\u2019t need to sync gigabytes of media files.<\/li>\n\n\n\n<li><strong>Consistency<\/strong> \u2192 Images always load from the live site, so the content looks exactly the same locally.<\/li>\n\n\n\n<li><strong>Automatic<\/strong> \u2192 You don\u2019t need to manually replace links; WordPress does it on the fly.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udc49 So in short: this plugin automatically redirects <strong>all local image and media URLs<\/strong> to your <strong>live site\u2019s domain<\/strong>, covering attachments, responsive srcset, and even post content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part of DevKit but I would like to share code snippet as well 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":46593,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Attachments redirect - Code DPlugins","description":"This is part of DevKit but I would like to share code snippet as well What This WordPress Plugin Does This plugin is designed for people who work on a WordPress"},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[118,59,117,116],"dpc_library":[],"class_list":["post-46406","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-attachment","tag-image","tag-img","tag-redirect"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46406"}],"version-history":[{"count":0,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46406\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46593"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46406"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46406"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}