As a website owner, you may sometimes need to make changes en masse. Whether you’re migrating a site between local and live servers, switching domains, or stripping all the shortcodes from your site, these bulk modifications can be time-consuming.
If you’ve ever used a word processor, then you may be familiar with the concept of ‘search and replace.’ You can create a similar effect in WordPress. If you need to change a particular element across your website, then this functionality can potentially save you hours of frustration and tedious busywork.
In this article, we’ll cover why you might want to perform a search and replace in WordPress. We’ll then share three ways that you can do so, ranging from user-friendly plugins to more advanced MySQL queries and WP-CLI commands. Let’s jump in!
Why You Might Need to Perform a Search and Replace in WordPress
There are a few different scenarios in which you can use search and replace in WordPress to streamline tasks. For starters, if you ever need to change your website’s URLs, then this feature can save you a significant amount of time.
Migrating from Hypertext Transfer Protocol (HTTP) to Hypertext Transfer Protocol Secure (HTTPS) or changing domains entirely are both examples of this. Search and replace enables you to update references to your old URLs with your new ones across your entire site, preventing errors, broken links, and other issues.
You should also consider performing search and replace whenever you move your WordPress site to a localhost installation, or when you migrate a local site to a live server. This will strip out references to the server you’re moving from and add in information for the one you’re transferring to, again preventing errors and inconsistencies.
When you’re updating hundreds of URLs manually, it’s easy to miss a single hardcoded reference. Broken links can damage your Search Engine Optimization (SEO) and lead to poor User Experience (UX). Since a search and replace is automated, you can update thousands of URLs while minimizing the risk of human error.
Additionally, some plugins spread shortcodes across your website. If you ever need to disable or delete such a plugin, then you should also remove all of its associated shortcodes.
Leaving orphaned shortcodes scattered across your site can damage your website’s performance, or cause errors. Each is also a loophole that hackers could potentially exploit, so search and replace can help keep your site secure.
3 Ways to Perform a Search and Replace in WordPress
A bulk search and replace can affect every part of your WordPress database. To make sure you don’t lose important data, you should create a WordPress database backup first.
There are several ways that you can perform a search and replace in WordPress. In this article, we’ll cover three options, including using a plugin, issuing MySQL queries, and interacting with WordPress via the command line.
1. Use the Search Regex Plugin
The most beginner-friendly way to perform a search and replace is with a dedicated plugin such as Search Regex. You can use it to modify almost any WordPress data, including posts, pages, comments, metadata, custom post types, and plugins.
Once Search Regex is installed, you can access it by selecting Tools > Search Regex in your WordPress dashboard. In the Search field, enter the term that you want to search for. You can also specify the content that this plugin should scan using the Source field:
When you’re happy with the query, you may want to perform a trial run. You can search your database without replacing any of the identified matches by clicking on Search. The plugin will display a list of positive matches:
This gives you a chance to note any results that you don’t want to replace, and to refine your search accordingly. Replacing matches that aren’t relevant to what you’re trying to accomplish could cause problems instead of solving them, so this is an important step.
You can now pick and choose which terms to replace, or apply this change across your website. To replace an individual reference, select its three-dot icon and then click on Replace Row. You can then enter the replacement text and click on Replace:
Alternatively, you can replace all positive matches by entering a new value in the Replace field, and then clicking on Replace all. Search Regex will update all the identified matches.
2. Perform a MySQL Query Via phpMyAdmin
You can also perform a search and replace using phpMyAdmin. By opting for this method, you can update your WordPress database without having to install and learn a dedicated plugin. However, phpMyAdmin does require you to perform search and replace via SQL queries, so you should be comfortable with the SQL domain-specific language.
You can access phpMyAdmin via your hosting account. If your host uses cPanel, select phpMyAdmin under Databases:
In the left-hand menu, select the database you want to modify. Then select the SQL tab:
You can now perform search and replace requests using SQL queries. They should use the following format:
update TABLE_NAME set FIELD_NAME=
replace(FIELD_NAME, 'Search', 'Replace');
In the following example, we’re searching our WordPress posts for the term “testing.” We’re then replacing each positive match with “example”:
update wp_posts set post_content=
replace(post_content, 'testing', 'example');
To run your SQL query, click on Go. After a few moments, phpMyAdmin will display the number of rows affected by this query.
Alternatively, phpMyAdmin also provides a default Search and Replace function. You can access it by selecting the Search tab in the phpMyAdmin console.
Note that, unlike the Search Regex plugin, there is no way to trial your query before you run it. If you replace instances of a term that are not relevant to your goals, it may result in unwanted side effects.
3. Run a wp search-replace Command Using WP-CLI
You can perform a search and replace using WP-CLI’s wp search-replace command. This will search all rows in a table for instances of a specific string, then replace all appearances of it with a second string that you define.
The wp search-replace command uses the following format:
wp search-replace <'old'> <'new'> <table>
In this command, old represents the string that WP-CLI should search for. New is the replacement string, and table is the database table where you want to perform your search. For example, if you wanted to replace apples with oranges in the wp_terms table, then you’d run the following command:
wp search-replace 'apples' 'oranges' wp_terms
Before you can execute a wp search-replace command, you’ll need to install WP-CLI. Once you’ve done so, connect to your server remotely via Secure Shell (SSH).
This requires your website’s SSH credentials, which you can obtain from your hosting account. They’re usually accessible via the Access, Root Access, SSH Access, or SSH Credentials section:
If you can’t find your credentials, contact your hosting provider for help.
The following steps may vary depending on your Operating System (OS), but to connect over SSH you’ll typically launch a console or Terminal window. You’ll then type the ssh command followed by your SSH credentials and the SSH server’s IP or address.
This might look something like:
ssh [email protected]
You’ll usually connect over port 22, but if you encounter an error then you may need to add -p, followed by your specific port number. For example:
ssh [email protected] -p20
When prompted, enter your SSH password to finish connecting to your server.
Next, you’ll need to change directory (cd) so the Terminal or console is pointing at your wp-config.php file. Assuming it’s located in the default public_html directory, your command should look something like this:
cd applications/<yourapplication>/public_html
You can now perform search and replace requests using the wp search-replace command.
Using the dry-run Command
Before you execute a search and replace in WordPress via the command line, you may want to perform a dry run. This will make sure you don’t accidentally replace any instances of a string that aren’t relevant to your goal.
The –dry-run switch will replace the specified strings, but won’t save your changes in the database, so you have a chance to revert them before they become permanent.
To use this switch, attach it to the end of your command like so:
wp search-replace <'old'> <'new'> <table> --dry-run
This step is highly recommended to prevent introducing new issues on your site.
Conclusion
Making hundreds or even thousands of manual changes to your WordPress database can be a time-consuming and frustrating process. By performing a search and replace instead, you can quickly and easily track down every instance of a string that needs updating and then apply the desired changes.
When performing a search and replace in WordPress, you can use:
- A plugin such as Search Regex.
- A MySQL query via phpMyAdmin.
- The wp search-replace WP-CLI command.
Do you have any questions about performing a search and replace in WordPress? Let us know in the comments section below!
No Comments