Find and replace data in WordPress with WP-CLI
When moving your site to a new domain, you can use commands available in WP-CLI to find and replace the domain name in the database. WP-CLI is a command line tool with simplified commands created specifically for WordPress sites. Besides domain names, you can use these commands to search and replace any other data in the database.
- An SSH app. We suggest PuTTY for Windows, or Terminal for macOS.
- Your FTP details. If you don’t have them already written down, you should find your FTP host, username, and password:
- For your FTP host, you can use either your domain name or your IP address.
- You can find your FTP username and password in your hosting account for Linux Hosting with cPanel or Managed WordPress Hosting (steps 1-7).
- Connect to your hosting account with SSH.
- Use the bash command ls to list files and folders, and cd and ../ to move through directories until you're in the directory with your WordPress files.
- (Optional) To see the replacements to be made without saving them to the database, use the following command:
wp search-replace 'https://colexample.com' 'https://mycoolnewbusiness.com' --dry-run
In the command above, you should replace:- https://colexample.com with the actual domain name you want to replace.
- https://mycoolnewbusiness.com with the actual new domain name which should replace the old one.
- To replace the data in the database, enter the following command:
wp search-replace 'https://colexample.com' 'https://mycoolnewbusiness.com'
Again, you should replace https://colexample.com with the actual old domain name and https://mycoolnewbusiness.com with the actual new domain name.
You'll see a success message confirming that the data has been replaced and a table with the number of changes for each database item.
More info
- Additional options for find and replace with WP-CLI
- Export and import a database with WP-CLI
- Want to know more about WP-CLI? Find developer specific info on their official site.
- If you don't want to use WP-CLI, you can find and replace data in WordPress with a plugin.