How to Search and Replace WordPress Domains Using WP-CLI

How to Search and Replace WordPress Domains Using WP-CLI

If you are migrating a WordPress website to a new domain or manually changing HTTP to HTTPS, you can use the WP-CLI search replace command to automatically update URLs in your WordPress database and minimize errors.

Compared to manual SQL queries, WP-CLI is safer because it can handle serialized data without damaging the WordPress data structure.

When Should You Use WP-CLI Search Replace?

Some conditions that usually require a domain search and replace:

  • Migrating a website to a new domain.
  • Changing HTTP to HTTPS.
  • Moving a website from staging to production.
  • Changing a subdomain to a primary domain.
  • Fixing URLs that still point to the old domain.

How to Perform a Domain Search and Replace Using WP-CLI

Before starting, ensure that the server or hosting you are using supports WP-CLI. If not, you can install it first: How to Install WP-CLI.

1. Open the WordPress Installation Folder

From the Terminal or SSH, access or open your WordPress installation folder, for example in public_html

cd public_html
Cara Search Replace Domain WordPress Menggunakan WP-CLI - WPGan - 1

2. Simulate Changes Using Dry Run

Before making changes directly to the database and configuration, it is recommended to run a simulation first:

wp search-replace 'domainlama.com' 'domainbaru.com' --report-changed-only --dry-run

The command above will not change the database and will only display the number of items that will be updated.

Cara Search Replace Domain WordPress Menggunakan WP-CLI - WPGan - 2

3. Run the Search and Replace

If the simulation results are correct, run the search replace command by removing the flag --dry-run

wp search-replace 'domainlama.com' 'domainbaru.com' --report-changed-only

Wait for the process to finish, and WP-CLI will update all data found in the WordPress database.

Cara Search Replace Domain WordPress Menggunakan WP-CLI - WPGan - 3

4. Changing HTTP to HTTPS

If you want to change all HTTP URLs to HTTPS, you can write your full website address, for example

wp search-replace 'http://domainlama.com' 'https://domainbaru.com' --report-changed-only
Cara Search Replace Domain WordPress Menggunakan WP-CLI - WPGan - 4

Make sure to use --dry-run before running the search replace command.

5. Verify the Search and Replace Results

To ensure the old domain is no longer in use, run the simulation again:

wp search-replace 'domainlama.com' 'domainbaru.com' --report-changed-only --dry-run

If no data is found, you will usually see the result 0 replacements to be made.

Cara Search Replace Domain WordPress Menggunakan WP-CLI - WPGan - 5

You can also check the Settings > General menu in the WordPress dashboard to ensure the website URL is already using the new domain.

FAQ

Is WP-CLI Search Replace Safe to Use?

Yes. WP-CLI can handle serialized data automatically, making it safer than performing a replace using manual SQL queries.

What is the Function of –dry-run?

The flag --dry-run is used to perform a simulation without changing the database. It is highly recommended to run this command before performing a search and replace.

Do You Need to Backup the Database Before a Search and Replace?

It is highly recommended to back up the database before running a search and replace, especially on a production website.

Written by

Muhammad Khoirun Nasikin

I enjoy tinkering with WordPress, hosting, VPS, web development, and various small experiments related to websites. I have over 3 years of experience in the web hosting industry.

COMMENTS

Loading...