How to Update Your Site URL With WP-CLI

There are plenty of reasons why you’d want to change the site URL. Switching from an internal domain to your live site address, creating a staging environment and so on. We’ll show you how to leverage the WP CLI commands available to get that done.

Before we start

Following the outlined steps in this guide will make changes to your database. As this affects how your site works, it’s important to create an export of the current database to fall back on if needed.

All commands shown need to be executed from the root of your WordPress installation. So if the site is located in the /public/ folder, first we  need to navigate there using the cd command, like this:

cd public

Doing the updates

As WP CLI is installed by default on all Bolts, the quickest way to update the URL in multiple locations is to use this to our advantage. We do this by leveraging the search-replace command.

The syntax is simple and looks like this:

wp search-replace [OLD_URL] [NEW_URL] --all-tables

This command will search through all tables within your database for strings that match [OLD_URL] and replace them with the new string, [NEW_URL].  By default, this command will only alter tables registered to the $wpdb object. But because we’ve added the –all-tables parameter, every table present in the database will be searched through. 

Be aware that this will replace all instances of [OLD_URL]. Using this on anything other than your full siteurl with schema (HTTP/HTTPS) might give results that aren’t easily reversible or wanted.

Elementor user?

When you’re using Elementor it’s important to realise that not all URLs are stored in plain text in your database. Even though WordPress stores everything in plain text, Elementor doesn’t. This means that we need to take an additional step in the process of updating the URLs. 

For this, Elementor has its own WP CLI commands available. Replacing URLs uses a similar syntax to what we used earlier:

wp elementor replace_urls [OLD-URL] [NEW-URL]

Just like before, substitute the [OLD-URL] with the old URL that was used, and [NEW-URL] with the URL we want to use. Once the command is done, it will tell you if the updates were successful or not.

For more information on Elementor’s relevant terminal commands, check out their official documentation.

Afterthoughts

This guide shows you how to update the URLs for your website in most use cases. If instances for the old URL are still appearing, there could be multiple factors at play. Things you can try: 

  • Clear your browser cache or use incognito mode for testing. 
  • Clear any server cache or CDN cache if they are present.
  • Run the earlier command with the –dry-run parameter. This will make no changes but will show you any URLs that haven’t been replaced for whatever reason.
  • Locate other plugins that could have obfuscated the URLs in the database.

And as always, should you have any additional questions please don’t hesitate to contact our support chat at servebolt.com!