The WP-CLI command line tool lets you efficiently search and replace content in your WordPress database. This is what is explained in this article.
Prerequisites:
- SSH access to your server.
- WP-CLI installed.
Follow the steps:
- Use a terminal to connect to your server:
ssh username@your-server-ip
- Navigate to the WordPress installation directory:
cd /path/to/wordpress
- Run Search and Replace Command:
- Use this command to search and replace text in your database:
wp search-replace ‘old-string’ ‘new-string’
- Example (changing site URLs):
wp search-replace ‘http://oldsite.com’ ‘http://newsite.com’
- Use this command to search and replace text in your database:
- Optional:
- Use –dry-run to preview changes:
wp search-replace ‘old-string’ ‘new-string’ --dry-run
- Use –all-tables to include non-standard tables:
wp search-replace ‘old-string’ ‘new-string’ --all-tables
- Use –dry-run to preview changes:
- Verify:
Check your site to ensure the changes are reflected correctly.
Note: Always back up your database before making changes.
This is how you can perform a search and replace in the WordPress database using WP CLI. If you need assistance, don’t hesitate to contact our support team.
Prefer using plugins? Discover How to Use a Plugin to Perform a Search & Replace in the WordPress Database