Depending on your circumstances, sometimes you need to update things in bulk on your WordPress website. Perhaps you just migrated from HTTP to HTTPS and you need to update all your hard-coded HTTP URLs in the database. Or perhaps you are changing domains and need to update all the database references. There are many scenarios in which knowing how to performance a bulk WordPress search and replace can come in very handy. It can also save you a lot of time, sometimes even hundreds of hours!
WordPress Search and Replace
There are are a lot of different ways in which you can perform a WordPress search and replace. In this post we will walk you through three different recommended options.
One of the first methods is to use a free plugin called Better Search Replace.
The Better Search Replace plugin is developed by the awesome WordPress team over at Delicious Brains and is inspired by the interconnect/IT PHP script in option 2 below. These guys know a thing or two about WordPress development. As of writing this, the plugin currently has 200,000+ active installs with a 4.6 rating. It is also actively maintained. You can download it from the WordPress repository or by searching for it within your WordPress dashboard under “Add New” plugins. Some of the features include:
- Serialization support for all tables
- The ability to select specific tables
- The ability to run a “dry run” to see how many fields will be updated
- No server requirements aside from a running installation of WordPress
- WordPress Multi-site support
There are hundreds of different use case scenarios. As you can see you in our example below, in the Better Search Replace options, we could easily search for any hard-coded HTTP references and update with the HTTPS version.
There is also a pro version of the plugin available which gives you even more features. You can check out their official documentation on how to better use the plugin. Another good alternative premium plugin
Another good alternative premium plugin is the Better Search and Replace Pro plugin from the team over at Delicious Brains.
A second option you have for running a WordPress search and replace is to use a free PHP script from interconnect/it called Search Replace DB.
This script has been around since 2011 and the developers actively update it. Interconnect/it is known for their great WordPress consulting work and development. To use the script, simply download the zip file, extract the folder called search-replace-db-master, and rename it to something secret of your choosing. In our example we renamed it to update-db-1551. Then upload it via FTP, SFTP, or SCP to your web server’s public directory. This is typically the same directory that contains your /wp-content folder. Then navigate to your secret folder in your browser, such as https://domain.com/update-db-1551.
The script will automatically attempt to find and populate the database field but you must check that the details are correct and that it is for the database you wish to carry out a search/replace operation on. You can click on “dry run” first to see what it will be updating/replacing. Then when you are ready click on “live run” which will perform the database updates and the WordPress search and replace.
It is also very important due to security reasons that you delete this script after you are done! You can click the “delete me” button. It you don’t, it could leave your website open to attacks. It is also recommend to double check on your web server and confirm that the folder/script has been completely removed.
And finally, the last recommend method for performing a WordPress search and replace is to do it directly with WP-CLI. This is for you WordPress developers out there. For our Google Cloud customers, we provide SSH access so you can use WP-CLI. If you are unfamiliar with WP-CLI you can check out our in-depth post on managing WordPress from the terminal.
Here is an example of the command:
wp search-replace 'http://example.dev' 'http://example.com' --precise --recurse-objects --all-tables
And a fourth option of course would be to manually run the search and replace queries directly in MySQL. But this should only be done by professional WordPress developers.