If you have migrated your website from HTTP to HTTPS, then you must be prepared to face few hiccups. One such issue is mixed content errors – If your website serve pages via https protocol, then all active mixed contents delivered via HTTP will be blocked by the browsers and the passive mixed contents are displayed by default (but left to the user to block it via preferences). It means, the user will see only partial content of the page or your website might break completely. All modern browsers such as Google Chrome, Internet Explorer and Firefox blocks mixed contents.
Well, we’ll get to back WordPress – If you are running a WordPress website, then you need to replace all the image links embedded in the post to serve via HTTPs. If you don’t, then it will result in Mixed Content Blocking.
How to identify Mixed content blocking?
You can either audit your website source code manually to identify HTTP URLs or Google Chrome’s inspect element and Firebug (in console) will show exactly the resources that are insecure.
Here’s the snapshot that shows how the web browsers react to Mixed insecure contents.
How to fix Mixed content blocking?
The best way to prevent mixed content blocking is to serve all contents as HTTPS instead of HTTP. It means, you need to replace all HTTP URLs with HTTPS or use protocol independent notation (//example.com). For more information, here’s a tutorial that explains best coding techniques that every HTML and CSS developer should know.
Again back to WordPress…
How to search and replace all image links in WordPress after migrating to HTTPs
There are couple of ways to do it. The first method is to manually search and replace image links in WordPress database using SQL Update query. The second method is to use popular WordPress plugins such as Better Search Replace, Search and Replace etc…
This tutorial will explain how to search and replace image links in wp_posts table using UPDATE query.
Caution: It's a good practice to take a backup of your database before performing the below steps.
In PHPMyAdmin
Login to PHPMyAdmin and select the WordPress database on the left, click on Query tab and enter the below query and hit Go.
UPDATE wp_posts SET `post_content` = REPLACE (`post_content`, 'src="http://your_domain_name.com', 'src="https://your_domain_name.com');
You also need to execute the below query to update the GUID for the images that are set as attachments.
UPDATE wp_posts SET `guid` = REPLACE (`guid`, 'http://your_domain_name.com', 'https://your_domain_name.com') WHERE post_type = 'attachment';
Note: Remember to replace your_domain_name with your actual website name.
In MySQL prompt
Login to MySQL and connect to your database.
mysql > UPDATE wp_posts SET `post_content` = REPLACE (`post_content`, 'src="http://your_domain_name.com', 'src="https://your_domain_name.com');
mysql > UPDATE wp_posts SET `guid` = REPLACE (`guid`, 'http://your_domain_name.com', 'https://your_domain_name.com') WHERE post_type = 'attachment';
Note: Remember to replace your_domain_name with your actual website name.
That’s it. The image links in all posts will be updated with HTTPS URL.