How to Change WordPress Site URL With WP-CLI

If you need to change the domain URL of your WordPress site, you can use WP-CLI, a powerful command-line tool for managing WordPress. Changing the site URL is a common task, especially when migrating your site to a new domain. Here’s a step-by-step guide on how to do it with WP-CLI.

1. Backup Your WordPress Database

Before making any changes, it’s crucial to create a backup of your WordPress database. This ensures that you can restore your site if anything goes wrong during the URL change process.

To export your database, use the following WP-CLI command:

1
wp db export

This command will create a SQL file with your database content that you can later import if needed.

To import the database backup, use the following command, replacing mydomain_dbname.sql with the actual name of your exported database file:

1
wp db import mydomain_dbname.sql

2. Rename All Instances of the Old URL to the New URL

Now that you’ve backed up your database, it’s time to change all instances of your old domain URL to the new one. To do this, you’ll use the wp search-replace command. Run the following commands to perform a dry run and then the actual URL replacement:

Dry Run

A dry run allows you to preview the changes without actually modifying the database. It’s a good practice to do this first to see what will be replaced:

1
wp search-replace 'olddomain.com' 'newdomain.com' --dry-run

Actual URL Replacement

Once you’re satisfied with the dry run results, you can proceed with the actual URL replacement:

1
wp search-replace 'olddomain.com' 'newdomain.com'

Additionally, if you’re switching from HTTP to HTTPS, you should also replace any instances of the old HTTP URL with the new HTTPS URL. Run this command:

1
wp search-replace 'http://domain.com' 'https://domain.com'

By following these steps, you can safely change your WordPress site’s domain URL using WP-CLI. Remember to make backups and exercise caution when running the wp search-replace command to avoid unintentional changes.

0%