WP-CLI is a powerful command line utility for WordPress, which is included by default with your hosting plan or server. It can be used to perform many of the actions you would normally complete in the WordPress dashboard, such as installing or updating plugins and themes, managing users, making changes to the site configuration, and much more.
Here we cover the steps to perform a search and replace in your WordPress database using WP-CLI. This can be used to update the website's URL and links in the event of your domain having changed or after cloning the site to a new domain or to replace a specific word or phrase throughout your site.
Related Articles
SSH into Shared Server
Find the Document Root of a Domain
Run search/replace
- Access the server via SSH or using the Terminal in cPanel
- Navigate to the site's document root using the cd command
cd public_html
REPLACE: public_html with your domain's document root if different. See the guide on finding the document root of a domain for guidance. - Create a backup of your database before proceeding
CAUTION: Due to the potential for unintended results when performing a search/replace, it is highly recommended to create a database backup so that changes can be easily reverted. - Run the following command to preview the changes that will be made
wp search-replace 'old' 'new' --dry-run --all-tables
REPLACE: 'old' with the word or phrase to be replaced, and 'new' with the word or phrase to replace it with.
NOTE: The
--dry-run
flag is used to run the search/replace operation and show the report, without saving the changes to the database. - If satisfied with the results, run the command again without the --dry-run flag
EXAMPLE: To update http:// links to https:// for a domainwp search-replace 'http://domain.com' 'https://domain.com' --all-tables
EXAMPLE: To change a website domain namewp search-replace 'https://domain1.com' 'https://domain2.com' --all-tables
CAUTION: When changing the domain name you should always includehttp://
orhttps://
as omitting these will result in all instances of the domain being changed, including email addresses. By including the protocol, you ensure you're only making changes to URLs.
TIP: Additional options, parameters, and examples of thewp search-replace
command can be found here.