Created
September 15, 2019 21:03
-
-
Save mizner/8bed09419cf7ecc4182548a545c2e982 to your computer and use it in GitHub Desktop.
MU Plugin: Remote Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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