4

In Wordpress 4.4 images get automatically a srcset attribute. My problem with this was the following (I solved it while I was writing this question, see my answer below):

  • in order to transit everything to https, I replaced all the src="http://... references in the posts table by src="https://... (I changed it later to src="//... for supporting both protocols);
  • the images on all the posts get the correct URL in the src attribute;
  • however in all the images that get the srcset attribute the URLs in it are always with http:// references.

Why does this happen? Why these URLs don't get my newly updated https:// beginnings?

Community
  • 1
  • 1
Armfoot
  • 4,663
  • 5
  • 45
  • 60

3 Answers3

5

If you don't want to change your WordPress Address (URL) to https then just put this code inside your active themes functions.php file

function codextent_ssl_srcset( $sources ) {
    foreach ( $sources as &$source ) {
        $source['url'] = set_url_scheme( $source['url'], 'https' );
    }
    return $sources;
}
add_filter( 'wp_calculate_image_srcset', 'codextent_ssl_srcset' );

** Also add this in top line of wp-config.php file.

$_SERVER['HTTPS'] = 'on';
Codextent
  • 400
  • 5
  • 7
4

After searching for a while in the wp-includes folder, the wp_calculate_image_srcset method in the media.php file uses these 2 lines:

$image_baseurl = _wp_upload_dir_baseurl();
$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;

And this $image_baseurl will actually form the new URL for the srcset attribute, i.e. even if the entire URL is in the wp_poststable and used in the src attribute, its beginning won't be used.

This means that if your base url in the wp_options table is still in http://, the images will get that protocol and won't show up by default by your browser when navigating in https.

For solving this, you just need to change the URLs inside the option_value in the wp_options table to https:// or just // if you still want to support both protocols (double slashed). You can do this in a single query:

UPDATE `wp_options`
 SET `option_value` = replace(option_value, 'http://', '//')
 WHERE `option_name` IN ('siteurl', 'home')
Community
  • 1
  • 1
Armfoot
  • 4,663
  • 5
  • 45
  • 60
  • Unfortunately, the query leads to Wordpress being unable to set its cookies, so wp-admin breaks :(it attempts to set them on path //host.name/ – alamar Nov 16 '16 at 19:28
  • 2
    @alamar a suggestion, if you may: once you have transferred all your content to http**s** and you are sure your public pages present all the images, you will not need the `//` any longer. At this time, you can run the same update where the replace string is `https://´. I no longer have the need to support both protocols and at that time this worked for me at least, but I believe that some side effects may take place... I did not mark this answer as accepted on purpose, so if you figure out how to solve both problems, I'll be glad to read it. – Armfoot Nov 22 '16 at 20:44
  • I have a similar problem, can you help? [How to convert srcset links from https to http?](https://wordpress.stackexchange.com/q/345569/64282) – Ooker Aug 21 '19 at 10:57
3

Change Following Setting in admin under Setting->General:

WordPress Address (URL) : https://yoursitename.com Site Address (URL) : https://yoursitename.com

And press [Save Changes] button. Finally refresh your page and your image will be displayed on your browser with correct srcset attribute.