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 perform 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 four different recommended options.
- Kinsta Search and Replace Tool
- Better Search Replace Plugin
- interconnect/it Search Replace DB PHP Script
If you are a Kinsta client, you can use our search and replace tool that is available from right within the MyKinsta dashboard. Follow the steps below.
Still looking for that perfect WordPress host?
- Fully managed
- Secure like Fort Knox
- Free migrations
- Ultimate speed
- Daily backups
- Google Cloud Platform
Under Sites click on “Manage” next to the site you want to run a search and replace on.
Click on Tools and you’ll find the “Search and Replace” tool at the bottom.
There are a number of different operations you can use this tool for, such as:
- Updating your HTTP URLs to HTTPS after a migration
- Moving site from www to non-www URLs or vice versa
- Updating old shortcodes, content, strings of text
In this example, we are going to show you how to update HTTP URLs to HTTPS.
- Enter in the search field the value you want to search for in the database, which in this case is our HTTP domain:
- Enter in the replace field the new value that should be used to replace the value that you are searching for. In this case it is our HTTPS domain,
- Ensure the “Dry Run” option is selected first, as this will count how many replacements will be made without actually making the replacements. Then click “Replace.”
Important: Ensure you don’t include any leading/trailing whitespace in either of the fields as this might produce undesirable results.
You’ll see a warning confirming that you want to run the command to calculate how many replace will be made. Click on “Replace” to confirm. Note: In “Dry Run” mode this will not make any database changes.
You’ll then see the total number of replacements that will be made.
You can then un-select “Dry Run” and click on “Replace” again to perform the search and replace, making changes in your database. Note: A backup is automatically taken when this is run (backup identifier:
beforesearchandreplace). So you can always revert back if needed.
You’ll then see one final confirmation of the number of replacements made.
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 300,000+ active installs with a 4.5 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.
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 recommended 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