Searching and replacing in WordPress has everything to do with a database. After all, that’s where the essential data is stored. It is simultaneously the place from which the information is called upon request. Since you never want your website to be stagnant, updating is pretty much a daily thing. Not only adding stuff on top, mind you, but also going back and making alterations to existing information. This is fine for tweaks here and there, but what if you need to “revise” an entire portion or a single word/script across your website? In that case, you need to know how to perform Search and Replace in WordPress.
Before you start
Although method 1 is more lenient, any changes you make using methods 2, 3, and 4 are permanent. Even worse, they can not be undone. Therefore, even if you know what you’re doing and the alteration to your WordPress database is minor, we strongly advise you backup a database in WordPress beforehand, if not your entire website.
1. Search and Replace text in WordPress pages and posts
Before you dive deeper, let us ask you this. Is the change you plan to make a part of a WordPress post, page, or custom post type? Though you can undoubtedly do so within a database, this may be faster, more user-friendly, and, frankly, much safer. What’s more, you likely already have the following plugin.
Using TinyMCE Advanced to find and replace text
If you’re already using TinyMCE Advanced, now known as Advanced Editor Tools, you can use its “Find/Replace” or “Search and Replace” features in Classic Editor. Moreover, through the power of Classic Block, you can also do so within Gutenberg Editor.
- After installation and activation, access the left sidebar of your WordPress Admin section.
- Go to Settings > TinyMCE Advanced or Advanced Editor Tools, depending on the version.
- Locate the Find and replace (Classic Editor) or Search and Replace (Classic Block) option on the list of “Unused Buttons”.
- Drag it to the desired place in your toolbar in the Editor Menu.
- Click on Save Changes.
- Open a post or page you want to edit.
- Click the button from step 3.
- Enter the text you want to find under “Find”.
- Type the word or phrase under “Replace with”.
- Click on Find, then Replace or Replace All.
2. Perform Search and Replace in a WordPress database via a MySQL query
You cannot work with databases without running into SQL (Structured Query Language) and MySQL/MariaDB, the two frequently used relational database management systems (RDBMS). Provided you’ve created a backup of your database and know the basics of SQL, you can do a text switch-up without additional tools. Here’s how that works:
1. Find the database name (Situational)
This step is unnecessary if you have extensive knowledge of your WordPress database. However, it never hurts to double-check, since you merely have to do this:
- Access your WordPress website using FTP.
- Head over to your root folder (also named public_html, www, or website-name).
- Open the file titled wp-config.php.
- Search for “database for WordPress”.
- Find the desired database name on the list that looks like:
define(‘DB_NAME’, ‘Database Name‘);
- We’ll use a database titled “wp_posts” for this demonstration.
2. Launch phpMyAdmin
With the database name in tow, open the cPanel (or a hosting-based equivalent) of your WordPress website. Now, do the following:
- Find and click the option titled phpMyAdmin, typically under “Database”.
- If requested (such as if you have multiple websites), select your website database from the list.
- Enter your database username and password. Select your language.
- You’ll gain access to the phpMyAdmin page. Click the SQL tab in the upper left corner.
3. Run a SQL code snippet
You’re now ready to execute a MySQL query through the following code:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'Text you want to search', 'Text to substitute it with');
Here’s a quick rundown of what the bits of information mean:
- TABLE_NAME — Name of the table from step 1. In our case, “wp_posts”.
- FIELD_NAME — Specify the field or the column the text you want to substitute is located in.
- ‘Text you want to search — Enter a word or a phrase you want the query to scour the database for.
- Text to substitute it with’— A phrase or a word you want the query to replace the previous text with.
We want to replace every mention of the phrase ‘our website’ with ‘WPThinker’. That would look like this:
update wp_posts set post_content = replace(post_content, 'our website', 'WPThinker');
Note. Though you backed your database up, we still suggest using a Dry Run option, since it can safely test the viability of your code snippet without making changes.
3. Searching and replacing database data using WordPress CLI
Has your web hosting provider provided SSH (Secure Shell) access? If so, you can follow these steps to perform a quick Search and Replace using CLI (command-line interface) in WordPress:
- Access your website via Terminal or an SSH client after entering your credentials like this:
- Enter your password for SSH when prompted.
- Switch directories, so you’re in the folder where changes need to be made. For example, going to root/public_html would look like this:
- Replace “hosting”, “your-website-name”, and “root” with actual names.
- You can now execute finding and replacing using the following command:
wp search-replace <'old word or phrase'> <'new phrase or word'> <table name>
- We strongly propose you perform a dry run for reasons we already explained. To do so, enter this command:
wp search-replace <'old word or phrase'> <'new phrase or word'> <table name> --dry-run
4. Perform a Search and Replace of database text using a WordPress plugin
It surprises no one that you can install a WordPress plugin to do the job safely, conveniently, and with little to no knowledge of SQL, FTP, or CLI. You don’t even need to seek the names of databases. Though we aren’t affiliated, we’ll show the procedure using Better Search Replace.
Here’s what to do after you finish installing and activating:
- In the sidebar on the left-hand side of the WordPress Admin dashboard, click on Tools.
- Select Better Search Replace.
- Stay in the Search/Replace tab. Configure options in this fashion:
- Search for — A word or phrase to find
- Replace with — Replacement phrase or word
- Select tables — Highlight one or more WordPress database table names
- Case-Insensitive — Seek exact match only (default) or all versions of that word or phrase
- Replace GUIDs — Not recommended for live websites. Used to replace GUID (Globally Unique Identifier)
- Run as dry run? — Safe option we already explained.
- Click the Run Search/Replace button at the bottom.