Skip to content

Instantly share code, notes, and snippets.

@jwenerd
Created February 2, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwenerd/e81616c1dc87a4d145a6 to your computer and use it in GitHub Desktop.
Save jwenerd/e81616c1dc87a4d145a6 to your computer and use it in GitHub Desktop.
<?php
/* The new responsive image feature in wordpress 4.4 causes image sourceset attribuet
to insert images as HTTP rather than HTTPS. This completely breaks images on sites where
the site is loaded ( and in sometimes force-loaded) to be served via HTTPS
The following plugin changes these images to be HTTPS if the site is served via HTTPS
*/
if ( is_ssl() ) {
add_filter('wp_calculate_image_srcset', 'psu_https_srcset_fix' , 100 , 5);
}
function psu_https_srcset_fix( $sources, $size_array, $image_src, $image_meta, $attachment_id ){
$http_site_url = get_site_url( get_current_blog_id(), '/', 'http' );
$https_site_url = get_site_url( get_current_blog_id(), '/', 'https' );
foreach( $sources as &$source ) {
if (strpos($source['url'], $http_site_url) !== false ){
$source['url'] = str_replace($http_site_url, $https_site_url, $source['url'] );
}
}
return $sources;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment