Skip to content

Instantly share code, notes, and snippets.

@jeff-kilbride
Forked from kopiro/filter-wp-uploads.php
Created March 28, 2018 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeff-kilbride/7477e9b95ffa05f3318b6f7a50db658d to your computer and use it in GitHub Desktop.
Save jeff-kilbride/7477e9b95ffa05f3318b6f7a50db658d to your computer and use it in GitHub Desktop.
Filter wordpress uploads
<?php
add_filter('wp_get_attachment_url', function($url) {
if (getenv('USE_S3_UPLOADS')) {
$url = 'https://' . getenv('AWS_S3_BUCKET') . '.s3.amazonaws.com' . str_replace(WP_CONTENT_URL, '', $url);
}
return $url;
});
add_filter('wp_calculate_image_srcset', function($sources) {
if (getenv('USE_S3_UPLOADS')) {
foreach ($sources as &$s) {
$s['url'] = 'https://' . getenv('AWS_S3_BUCKET') . '.s3.amazonaws.com' . str_replace(WP_CONTENT_URL, '', $s['url']);
}
}
return $sources;
});
add_filter('the_content', function($content) {
if (getenv('USE_S3_UPLOADS')) {
$content = preg_replace(
"~https?\:\/\/www.site.com\/wp-content\/uploads~",
'https://' . getenv('AWS_S3_BUCKET') . '.s3.amazonaws.com/uploads',
$content
);
}
return $content;
}, PHP_INT_MAX);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment