Replacing Image Links in WordPress After Installing an SSL Certificate
After installing an SSL certificate on your site to amp up your security, you may notice your images suddenly don’t display at all. Don’t panic – just read on for the fix.
When you install an SSL certificate, like the free one from Let’s Encrypt we wrote about recently, your site’s address adjusts slightly and even a slight adjustment means some elements on your site will probably break. Your site sees a conflict with the URL where it’s programmed to run and the old URL some content such as images are using. Everything has moved to your https URL, but your images still point to http.
This means you need to change all the image links and replace http with https. What a pain in the rear, right? Fortunately, you don’t have to track down and edit every single image you have uploaded. You can make the switch manually in your database or you can opt for the easier and safer route with a plugin.
If you have successfully updated your site with your new secure URL, but the images aren’t all functioning properly, read on to see how you can manually make the changes you need in your database or view the services and plugins you can use instead which are safer and quicker to use.
Manual Search and Replace with Queries
It’s possible to change your image links manually in your database, though, be forewarned: It can be easy to make a mistake and further break your site. While I’ll still show you how you can make these changes for completeness, it’s important to make note of the risks and it’s up to you to decide whether you should proceed.
I recommend you use a trusted plugin instead since there’s a bit less of a chance to make a mistake. Still, you should use the right amount of caution when making any changes to your site.
It’s a good idea to backup your site completely before making any changes such as editing your site’s image links. To get details on how to do this, check out some of our other posts such as How to Backup Your WordPress Website (and Multisite) Using Snapshot and 7 Top Premium and Freemium WordPress Backup Plugins Reviewed.
That being said, if you find yourself in a rare situation where you need to take a look at your database and tables to sniff out some broken image links, you can do this with a database search and replace query.
Log into phpMyAdmin and click on your site’s database that’s listed on the left, then click the Query tab. In the SQL query on database box toward the bottom of the page, enter the following query, the click Go:
Be sure to update
www.your-site.com to reflect your real domain name and change the
wp table prefix of
wp_posts if you don’t use the default and have previously changed it. If you’re using Multisite, be sure to also run this query afterward if you need to update images there:
wp_2_posts to match the correct site ID as it corresponds to your subsites. You need to run this query for each of the sites in your network. This many not always be necessary, but if your network is set to use sub-directories and your sites aren’t mapped to use different domains, you may find you need to change your image links.
You also need to run the query below to update the GUID for images that are set as attachments:
Similar to the first example, you need to replace the domain with your actual URL and change the table prefix if you don’t use the default setting. You may also need to run this query for all the sites in your network:
Don’t forget to replace the
wp_2_posts with the actual site ID number for the sites in your network.
Your images should all be updated to reflect your new URL using https. If it doesn’t work, sometimes trying again does the trick. It also helps to run each query one at a time, rather than attempting to run them all at once.
Search and Replace Image Links with Plugins
Using a plugin to search for and replace all the images on your site is a lot easier than doing this manually. It’s also a lot less likely that you would make a mistake that could ruin your site. Still, it’s important to double check everything you type in since you could still make a mistake.
These plugins are regularly updated so you don’t have to worry about being left behind with an outdated plugin. While not all of these plugins were intended for use on Multisite, they should still work when activated on a site-by-site basis. No matter which option you choose, they’re all quality plugins that should work well for your sites.
Better Search Replace is one of the plugins on this list that specifically has Multisite support. It has a simple design and not a lot of features or settings, but at the same time, it’s the only options most people ever need.
All you need is a running installation of WordPress and you can use this plugin which incidentally is as easy to install as it is to use. The best feature of this plugin is that you’re able to do a test run to see what tables are affected by the options you choose before you actually apply the changes. This means you’re less likely to make a mistake which makes it a valuable tool to have in your toolbox.
The Search and Replace plugin is able to handle a lot more than just replacing image links on your site. You can also use it to change your site’s URL and domain, backup and restore your database, and change your table prefixes from the default
This plugin also has the ability to do a dry run to test out your proposed changes before pushing them through. It can be easier to avoid mistakes when you can see the results of your selections before having to worry about undoing your last changes.
Although, there is one requirement that this plugin needs to run which is a PHP version of 5.4 or newer. Search and Replace does install like most other plugins and there aren’t any extra steps involved. It also includes Multisite support.
WP Migrate DB is normally used for migration your site, but you can also use it to search for and replace old image links. It’s also great for migrating a locally developed site to a live setup.
It’s easy to install and doesn’t have any special requirements to run. While many wouldn’t necessarily think of this plugin initially when wanting to replace image links, it works great and it’s the first choice for this task for many WordPress users.
Images can be tricky business when switching your domain to include an SSL certificate. You can update your
siteurl all you want, but those pesky images are still going to stay broken. The only solution to fix this issue is to update the image links yourself, although, it should be a breeze with the plugins in this post.
For other broken links on your site that aren’t images, there’s a special fix for this as well, which you can check out in our post How to Fix (Or Remove) Broken Links on Your WordPress Website.
Have you been able to successfully change your image links? What plugin did you use or which one is your favorite? Do you have one you would rather use instead? Share your experience in the comments below.