WP Umbrella Logo

Mastering Search-Replace Command Using WP CLI  

Basilis Kanonidis

Need to update URLs across your WordPress site? It’s a headache to do it manually. The WordPress CLI search-replace command makes this much easier. One simple command and you can change text anywhere in your database—no clicking through pages or risking database errors. 

In this article, we’ll discuss the Search and Replace function of WordPress CLI and how you can use this feature.

What is WP CLI?

what is WP CLI

WP CLI lets you manage WordPress from your command line instead of clicking through the admin dashboard. With simple commands, you can update your site, back up your database, or publish posts. It’s especially useful for repetitive tasks and managing multiple sites. What takes several clicks in the dashboard can be done with a single command.

What is the Search and Replace Function?

The WP search-replace command is one of WP CLI’s most useful features. It searches for a string across all the rows in a selected database table, and bulk replaces it with another string. One notable feature of the WP search-replace command is its ability to unpack JSON payloads and handle PHP serialized data.

When Should You Use the Search-Replace Function?

There are many situations where the search-replace command can be a lifesaver, including:

  • Fixing encoding errors: Correcting corrupted characters from encoding issues to restore proper text formatting.
  • Site migrations: When moving your or your client’s site to a new host.
  • Launching a staging site: When pushing a development or staging site live.
  • Bulk URL updates: Updating links across your site when URL structures change due to rebranding or system updates.

How to Use the Search-Replace Function of WP-CLI

Here are a few simple steps to help start with using the Search-Replace function.

Prerequisites:

  • An SSH app. 
  • Your FTP details, like your FTP host, username, and password
    • Use either your domain name or your IP address for the FTP host.
    • Your FTP username and password can be found in your hosting account.
  • Install WP-CLI if you use VPS or a dedicated server.  

Note: Always back up your website before making changes to the database. This way, you can always restore if there are any errors.

Step #1: Use SSH to Connect Your Hosting Account

Use a terminal to connect to your server.

ssh username@your-server-ip

Step #2: Locate the Directory in Your WordPress Files

Using the bash command ls, list files and folders. Use the cd and ../ to navigate until you’re in the directory within your WordPress files.

Step #3: Run the Search and Replace Command

Use the command to search and replace text in your database. Here’s an example

wp search-replace ‘old-string’ ‘new-string’.

Here’s an example of searching and replacing a URL:

wp search-replace 'https://domainname.com' 'https://newdomainname.com'

(Optional) To preview replacements, use the following command:

wp search-replace 'https://domainname.com' 'https://newdomainname.com' --dry-run

Once done, a success message will appear confirming the data replacement. 

Step #4: Verify the Changes

Review your website to confirm the changes have been applied correctly.

Here are a few parameters you can use with the search-replace function:

CommandDescription
<old>A string to search for within the database.
<new>Replace instances of the first string with this new string.
<table>…List of database tables to restrict the replacement to. Wildcards are supported, e.g. wp_*options or wp_post*.
–dry-runA string containing the user’s URL for the user’s website.
–networkSearch/replace through all the tables registered to $wpdb in a multisite install.
–all-tables-with-prefixEnable replacement on any tables that match the table prefix even if not registered on $wpdb.
–all-tablesEnable replacement on ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides –network and –all-tables-with-prefix.

For the complete list, click here.

Bonus: Use Regular Expressions (RegEx) in WP search-replace to quickly find and update text with precision. Symbols like ^ (start of a line) and $ (end of a line) help with complex searches. Wildcards like . match any character, while * allows for zero or more repetitions. Modifiers like i make searches case-insensitive.

wp search-replace "(?<!@)olddomain\.com" "test.com" --regex --precise --skip-plugins --skip-themes --allow-root

Final Thoughts

Managing WordPress databases doesn’t have to be complicated. With WP CLI’s search-replace command, you can make bulk changes safely and quickly.

If you manage multiple websites and need a website monitoring tool, try WP Umbrella. It offers bulk actions (safe updates, database optimization) automated backups, uptime monitoring, security checks, and detailed reporting—all in one place. Start your 14-day free trial today; no credit card required.

Frequently Asked Questions

Do I need technical knowledge to use WP CLI search-replace?

You’ll need basic command line skills and SSH access to your server. While the commands are straightforward, you should be comfortable using the terminal, know SQL, and understand the basics of WordPress databases. Make sure you have WP CLI installed and a text editor ready.

What are some best practices for search-replace commands?

Since search-replace commands make direct database changes, you need to be careful when using them. Always start with the –dry-run option to see what would change without making changes. This helps you verify the results before touching your database. Add –report-changed-only to keep the output clean and readable. You can use both options together to get a clear picture of what’s going to change.

Can I undo a search-replace operation? 

You can only undo changes by restoring from a backup. That’s why backing up your database before running any search-replace commands is crucial.

What are WP CLI attributes?

WP CLI has three main attributes: commands, parameters, and flags. Commands like ‘wp core update’ or ‘wp plugin install’ tell WP CLI what to do. Parameters are name-value pairs that can be built into CLI or defined as variables. Flags are additional options you can add to customize how a command runs. Understanding these helps you get the most out of WP CLI’s features.