Not working for new srcset attribut? Possible workaround
-
Since version 4.4, WordPress has support for responsive images using a list of different files on srcset attributes on img element. The browser checks this list against the UA width and UA type and chose the best fitting one, instead de the one on the common src attribute
But when we used WP CDN Rewrite (both dreamhost and wpengine hosted sites), those image URLs on srcset are not rewritten to use your CDN (sub)domain.
We used the following function as a workaround. It uses the new wp_calculate_image_srcset filter to force WordPress to use CDN subdomains on srcset list.
We’re not sure this is the proper of most efficient way of doing it, so, we welcome anyone giving a try, testing it and contributing making pull request with a better solution on https://github.com/2aces/wordpress-srcset-cdn.
function aafd_cdn_srcset($sources){ foreach ( $sources as $source ) { $sources[ $source['value'] ][ 'url' ] = str_replace('https://www', 'https://static1', $sources[ $source['value'] ][ 'url' ]); // you MAY use external domains as well // $sources[ $source['value'] ][ 'url' ] = str_replace('https://www.example.com', 'https://static.examplecdnprovider.com', $sources[ $source['value'] ][ 'url' ]); } return $sources; }
- The topic ‘Not working for new srcset attribut? Possible workaround’ is closed to new replies.