Skip to content

Instantly share code, notes, and snippets.

@mizner
Created September 15, 2019 21:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizner/8bed09419cf7ecc4182548a545c2e982 to your computer and use it in GitHub Desktop.
Save mizner/8bed09419cf7ecc4182548a545c2e982 to your computer and use it in GitHub Desktop.
MU Plugin: Remote Images
<?php
/**
* Plugin Name: Remote Images
*/
if(!in_array($_SERVER['SERVER_NAME'], ['EXAMPLELOCALDOMAIN.test', 'ANOTHEREXAMPLE.test'])) {
return;
}
// Replace src paths
add_filter('wp_get_attachment_url', function ($url)
{
if(file_exists($url))
{
return $url;
}
return str_replace(WP_HOME, 'https://www.some-production-site.com', $url);
});
// Replace srcset paths
add_filter('wp_calculate_image_srcset', function($sources)
{
foreach($sources as &$source)
{
if(!file_exists($source['url']))
{
$source['url'] = str_replace(WP_HOME, 'https://www.some-production-site.com', $source['url']);
}
}
return $sources;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment