You need to have WP-CLI installed on your web server to go thru this tutorial. Many hosting companies now provide this service by default. If you don’t have WP-CLI on your server, you need to contact the hosting company and ask them to install it,
You will also need SSH access to your server.
This tutorial is for WordPress developers, but it’s easy to follow for beginners.
Safety-first: remember to backup your database before starting.
What is WP-CLI?
WP-CLI is short for WordPress Command Line Interface. Using a terminal (Windows, Mac, Linux) you can review and change anything in your WordPress database using these commands.
Search and Replace URLs in the WordPress database in just a few seconds
WordPress stores internal URLs as absolute addresses – the full address including the http / https protocol, like https://www.example.com/. This will cause problems when you need to change your domain name (in this example, changing from a local site to an online domain). You will need to change all the URLs in WordPress.
Thanks to WP-CLI you can make this change in a few seconds and without installing a plugin. The command line interface is faster and more efficient. A WordPress plugin will be slower to implement and may be limited by your PHP configuration. It will also leave traces in your database and on your server too.
The first step is to connect to your server remotely using a SSH client terminal. Go to the directory containing the WordPress installation, and type in the following command:
wp search-replace 'http://example.local/' 'https://example.com/'
The syntax is the Old URL http://example.local/ followed by the New URL, https://example.com/.
Hit Enter to run the command.
Wait a few seconds depending on the size of your database.
A list of results of your search-replace will be shown on the screen in tabular form.
We can repeat the search replace using URLs without slashes at the end like this:
wp search-replace 'http://example.local' 'https://example.com'
And that’s it, your site is migrated!
You will notice that in the example we also changed the protocol from http to https. The command can be very useful when you move to a secure https address. But please note that this is not the only thing than needs doing to move to https, you also need to set up a SSL certificate for example.
The wp search-replace command has a number of options such as:
- –skip-tables=<tables> to skip certain tables in the database. Separate table names using commas. You can use wildcards such as “wp_*options” or “wp_post*” (note the asterisk wildcard)
- –dry-run allows you to text the search / replace without actually changing the database. You simply get the report of changes that would have been made if the search replace command was not a dry run
You can read the WP-CLI handbook , to learn how to create your own scripts and run more mass changes on your WordPress sites.