Do a Proper Find/Replace of URLs on a WordPress Install with WP-CLI

When migrating or moving sites live we often need to globally find and replace specific URLs within the WordPress database. With WP-CLI that is easy!

Note: on WPENGINE you may have to request WP-CLI be enabled 

Do a Search-Replace Test Run

The best idea is to first do a test run by entering:
wp search-replace OLDURL NEWURL --dry-run

If successful that should give you an idea how many items you’re going to be affecting. If it returns zero then you may not need to to run the search/replace or you may have constructed it incorrectly. It goes without saying that before doing anything live that you have taken a restore point…

Search-Replace URLs for Real

If you are confident of the search-replace then now do it for real, we like to use…
wp search-replace OLDURL NEWURL --precise

You can see all the search-replace options in the WP-CLI documentation https://wp-cli.org/commands/search-replace/

Protocol and Precision Are Key!

Keep in mind when doing URL replacement that protocol matters! When migrating a WordPress site from HTTP to HTTPS we like to first run a command that doesn’t replace the protocol. Such as:
wp search-replace limecuda.wpengine.com limecuda.com --precise

And then later on in the migration do a Find / Replace from HTTP to HTTPS
wp search-replace http://limecuda.com https://limecuda.com --precise

Have a favorite WP-CLI trick or better search/replace technique? Comment below!

Exit mobile version