Created
January 19, 2018 12:38
-
-
Save GhazanfarMir/c8172615d461faaea450a78fe6244f68 to your computer and use it in GitHub Desktop.
Forces CDN subdomain for srcset file list on img element
This file contains hidden or 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 | |
/** | |
* forces CDN subdomain for srcset file list on img element | |
* | |
* Forces Wordpress to use CDN subdomains on srcset list | |
* used since version 4.4 for responsive images support | |
* | |
* Wordpress, Responsive Web Design, Responsive, Responsive Images, RWD, 4.4, CDN, Subdomains, performance | |
* | |
* @author Celso Bessa <[email protected]> | |
* | |
* @since 1.0 | |
* | |
* @param array $sources Source files data from wordpress media class (media.php) | |
*/ | |
// you MUST change static1 to your CDN subdomain as needed | |
function aafd_cdn_srcset($sources){ | |
foreach ( $sources as $source ) { | |
$sources[ $source['value'] ][ 'url' ] = str_replace('http://www', 'https://static1', $sources[ $source['value'] ][ 'url' ]); | |
// you MAY use external domains as well | |
// $sources[ $source['value'] ][ 'url' ] = str_replace('http://www.example.com', 'https://static.examplecdnprovider.com', $sources[ $source['value'] ][ 'url' ]); | |
} | |
return $sources; | |
} | |
add_filter( 'wp_calculate_image_srcset', 'aafd_cdn_srcset'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment