Skip to content

Instantly share code, notes, and snippets.

@INDIAN2020
Forked from jwenerd/https-srcset-fix.php
Created December 4, 2023 06:44
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 INDIAN2020/754942be4e649276aa9168c5dbb03c3d to your computer and use it in GitHub Desktop.
Save INDIAN2020/754942be4e649276aa9168c5dbb03c3d 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