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 bysrc="https://...
(I changed it later tosrc="//...
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 withhttp://
references.
Why does this happen? Why these URLs don’t get my newly updated https://
beginnings?
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
** Also add this in top line of wp-config.php file.
After searching for a while in the
wp-includes
folder, thewp_calculate_image_srcset
method in themedia.php
file uses these 2 lines:And this
$image_baseurl
will actually form the new URL for thesrcset
attribute, i.e. even if the entire URL is in thewp_posts
table and used in thesrc
attribute, its beginning won’t be used.This means that if your base url in the
wp_options
table is still inhttp://
, 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 thewp_options
table tohttps://
or just//
if you still want to support both protocols (double slashed). You can do this in a single query: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.