Often times, you can develop a need to search and replace words or strings on your WordPress website for any of the following reasons:
- You want to rename a keyword. In this scenario, you chose a keyword, say keyword A, a few months ago because you thought it was good for your website’s SEO. However, now, after a few months have passed, you realize that another keyword, say keyword B, is more viable for your WordPress site. Now, if this keyword is spread across your website, then a manual way of replacing it will involve you opening up a 100 MB database file, changing it and then re-uploading it. (Don’t worry, we promise there are better ways of doing it and we will talk about them below).
- You realized that you have been repeatedly making a grammatical (or a spelling) mistake or a typo across your website. For example, you have been using “out of the green” instead of “out of the blue” and now you want to search for its occurrences everywhere on the website and replace them.
- After a WordPress update, some weird looking characters have started to appear on your website and you want to get rid of them.
- You added a URL or a set of sentences to some of your posts but you have no idea specifically which posts they were. For example, you added an author’s bio at the end of some of your articles and you want to update it.
Now, we did mention a few scenarios where you can need a “search and replace” functionality, but it is okay if your scenario is a bit different from the aforementioned. No matter what the scenario, going through each and every file and searching manually is a tedious job and nobody wants to do it. Fortunately for us though, there are ways you can quickly and efficiently find and replace stuff on your WordPress website. One big disadvantage, however, is that in case you make a mistake while performing the find and replace, you won’t be able to undo your changes. So consider backing up your WordPress site before proceeding to prevent any serious data loss.
Option 1: Using a plugin
Plugins make the life of WordPress website owners easier every day. If you don’t have any development experience or just don’t want to go through the pain of running SQL queries, then you can just simply use plugins like Better Search Replace or Search & Replace to get the job done. The first thing you need to do is install the plugin. You can do that by accessing your WordPress administrator dashboard and navigating to Plugins -> Add New section.
In the search field, enter Better Search Replace and install it.
Click Activate once the installation finishes and the plugin will be implemented. You’ll be able to locate it via Tools-> Better Search Replace section.
Next, you will need to enter the information that you wish to change:
- Search for section should have the phrase or string that you wish to change.
- Replace with should hold the value that you wish to be placed instead of the above used one.
- Select tables allows you to choose which tables of your database will be affected. If you want to select all them, you can use CTRL+A. Or, you may select individual tables by holding CTRL and clicking them
- If you want the action to be case-insensitive, make sure to tick the box.
- Tick the Run as dry run? box if you wish to review the changes first before applying them.
If you run the plugin without the dry run option, it will immediately make changes in your database and you will see similar results:
Congratulations! You have successfully edited all your WordPress database entries using a plugin.
Option 2: Using MySQL Query
It is also possible to perform a find and replace operation using phpMyAdmin, which is usually located in the hosting control panel.
In case you have a lot of databases under your account, you can open the wp-config.php file to find the database name that your website uses.
When you enter phpMyadmin, select the database that WordPress uses and click on the SQL tab.
This is the syntax of the query that you will use to perform the operation:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text to search, 'text to replace it with');
For instance, if you wanted to update the posts table, you could do so by using this query:
update wp_posts set post_content = replace(post_content, 'Find this text' , 'Replace it with this');
Note: wp_ should be replaced with the proper prefix used by the tables of your WordPress database.
After a few moments of processing, you will be shown exactly how many rows got affected.
Great job! You have learned how to manipulate the information in your WordPress database using SQL queries in phpMyadmin.
Doing most things on WordPress is pretty easy and the find and replace operation is no exception. If you know your way around SQL and don’t want to install a plugin to help you get the job done, then the second way is ideal for you. However, if you don’t want to get your own hands dirty and just want a plugin to do the job for you, then option one is the way to go.