Skip to content

Instantly share code, notes, and snippets.

@jwenerd
Created February 2, 2016 19:44
Show Gist options
  • Save jwenerd/e81616c1dc87a4d145a6 to your computer and use it in GitHub Desktop.
Save jwenerd/e81616c1dc87a4d145a6 to your computer and use it in GitHub Desktop.

Revisions

  1. jwenerd created this gist Feb 2, 2016.
    19 changes: 19 additions & 0 deletions https-srcset-fix.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    <?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;
    }