Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022
Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022 Black Friday SALE 50% OFF on the entire line of Qi products Discount code: BLACKFRIDAY2022

Get Qi Theme with 150 superbly designed WordPress demos

Get Qi Theme
Qi Theme
Sign up for our newsletter and be the first to get all the latest Qode news and updates.
BACK TO TOP

How to Perform a Search and Replace on Your Whole WordPress Site

How to Perform a Search and Replace on Your Whole WordPress Site

Have you ever wanted to replace a word or phrase across all your posts and pages? Or has your company changed something about its brand and needs that to be reflected on their entire site? On the other hand, if you ever migrate your site from one domain to another, clone from or to a local-hosted environment or add SSL support to your existing website, you will need to change all the URLs as well. Manually going through each and every page or post for edits would be both exhausting and time-consuming. Knowing how to quickly search and replace a word or phrase across your entire WordPress website is incredibly useful. So, we put together this tutorial to show you how to do this in a few straightforward steps:

How to Perform a Search and Replace

There are many ways to search and replace content across your WordPress site but we will only show you a few. Since this is generally the domain of developers and professionals with coding knowledge we have selected a couple of the less advanced methods. You can choose from them according to your skill level. However, if you still find yourself lost or unable to follow the steps we suggest you seek professional help. Trying any of these methods may break your website, so please follow the instructions carefully and make a database backup beforehand.

Search and Replace Using a WordPress Plugin

One of the reasons why WordPress is so popular is the sheer number of plugins, free or paid, which you can choose from to get added features. They allow you to extend the functionalities of your site in almost any area, including find and replace. For this tutorial, we’re going to use the Better Search Replace plugin. Its features will let us narrow the search by selecting specific database tables and we can even test the search and replace feature before executing it.

Begin by installing the plugin. To do that go to Plugins > Add New and search for the Better Search Replace plugin then click on Install Now. Activate the plugin afterward.

Better Search Replace plugin

You will be able to access the plugin options by going to Tools > Better Search Replace. When you open it, make sure you’re on the Search/Replace tab. From there you can type in the phrases or words to Search for and Replace with. You can select which tables to search in and tick different options. After you’ve done that, click on the Run Search/Replace button. If you tick the Run as dry run? option, you will get a list of search results but no changes will be made.

Better Search Replace Tools
Find and Replace on Your Whole WordPress Site

If you want to change the database, make sure that this option isn’t ticked. And after you make changes, a notice at the top of the page will appear, showing how many tables were searched and the number of changes made.

How many tables were searched

Before we go further, we need to take a look at a few database tables in more detail.

During a standard WordPress installation, 12 tables are automatically created inside your database: wp_posts, wp_postmeta, wp_comments, wp_commentmeta, wp_options, wp_users, wp_usermeta, wp_terms, wp_termmeta, wp_term_relationships, wp_term_taxonomy and wp_links. If you want to learn in-depth about database tables then you can check out resources like the WordPress Codex. And if you have plugins installed, your database could contain additional plugin-related tables. Now, of the original 12 tables, only some are relevant to us now.

The wp_posts table is probably the most important table in your WordPress database. It contains information about your posts, pages, menu items, attachments, and custom post types. It also contains post ID, content, and type, as well as author ID, date of creation, excerpt, title, comment status (if you enabled post comments), publish status (e.g. ‘pending’, ‘publish’ or ‘private’) and its GUID.

The wp_comments table, as the name suggests, contains all the comments which were made and information about them. It includes comment content, author of the comment, their email, URL, and IP address, date when the comment was posted and its type (comment, pingback or trackback).

The wp_users table contains information about users managing your WordPress website. Among other fields, there are user ID, username and password, user email, and URL.

The wp_options table contains information about active plugins and their settings, as well as theme settings, widget content, and cache. It contains the name of the plugin setting stored, its value, and if it is autoloaded.

Now we’re going to circle back to our plugin options to explain what Replace GUIDs? means.

The GUID column is a column inside the wp_posts table. GUIDs or Globally Unique Identifiers are constants used when generating RSS feeds. WordPress uses GUIDs to determine if two posts are the same and avoid post duplicates from showing. For example, if you’ve changed the URL, title, or some of the content of an old post, the GUIDs stop it from popping up on the RSS feed as a brand new post. So, changing post GUIDs can cause post duplicates to appear as new posts in your readers’ feed, which can be annoying and lead to them unsubscribing. Unless you are switching domains or working on development sites, we would urge you not to make any changes to the GUID column.

Qode WordPress Themes: Top Picks
Bridge WordPress Theme Banner
Bridge

Creative Multi-Purpose WordPress Theme

Stockholm WordPress Theme
Stockholm

A Genuinely Multi-Concept Theme

Startit WordPress Theme
Startit

Fresh Startup Business Theme

Biagiott banneri
Biagiotti

Beauty and Cosmetics Shop

Running Queries in phpMyAdmin

Another method of running search and replace across your WordPress website is via queries that affect your database. It’s very effective, but we suggest it for advanced WordPress users only.

To begin, log in to cPanel and find the phpMyAdmin option within the Databases section.

phpMyAdmin

A new window will open, showing all your databases. From the menu on the left, select the database for the site you want to change.

However, if you aren’t sure what your database name is, you can find that out by inspecting the wp-config.php file. To do so, connect to your server using your FTP credentials and navigate to your root WordPress directory (usually called public_html). Locate the wp-config.php file within the directory, right-click it, and select View/Edit from the dropdown menu.

WP Config Edit

Look for the piece of code that looks like this:

define( 'DB_NAME', 'your-database-name' );

and you will find the name of your database.

Find the name of your database

So now that we know which database we’ll be editing, we need to go back to cPanel and phpMyAdmin in order to run our search and replace.

There are two ways you can do this—manually or automatically. Both methods require you to run an SQL query, the difference lies in the query structure and how you proceed when the results are returned.

  • Manually replacing the words/phrases

For this method you need to make an SQL query, i.e. search your database for a word/phrase and manually replace each returned result.

Example:

From phpMyAdmin, select your database from the menu on the left, and click on the SQL tab in the top menu. In the window below, insert the SQL query.

Before we proceed, let’s take a look at the query we’re going to use. It is an offshoot of a generic query:

SELECT * FROM table_name WHERE (field_name LIKE 'your-phrase-with-a-wildcard');

This query returns all entries from a database table called table_name where a column field_name contains a pattern called your-phrase-with-a-wildcard. Now let’s break down the wildcard phrase.

SQL supports two wildcard operators which are used in conjunction with LIKE to describe a pattern you wish to search for. Those are the percent sign (%) and the underscore (_). The percent sign represents zero or more characters, while the underscore represents exactly one character. For example, the phrase %work% represents a pattern that has 0 or more characters followed by the word work, after which there are also 0 or more characters. Blank spaces are also considered characters. So, querying for the phrase %work% will return results that include the word work by itself, in a sentence, or a paragraph.

To illustrate, we’re going to look for work in our site posts, so that query looks like:

SELECT * FROM wp_posts WHERE (post_content LIKE '%work%');

To run the query, press Go. This will return all entries from the wp_posts table in our database which contain the word work in the post_content column.

Run the query

The list of results will be shown underneath the query window. To edit a result, you will need to click the Edit button within a result row and manually change (i.e. replace) each result.

Click the Edit button

When you click Edit, a new window will open. On the left, locate the post_content column and then click on the Value entry in it. Find the word/phrase you were looking for (in our case it is work) and edit it. If you are unable to find it with the naked eye, you can search for it with CTRL + F. All the matches will be highlighted so you can spot them easily.

Find the word you were looking for

When you’re done, scroll to the end of the page and click Go to save the change.

If the edit was successful you will get a confirmation message.

Confirmation messag

You need to repeat this process to edit all the other SQL query results. Making changes manually may be slow-going but it gives you greater control over the edits. If you use this method to run search and replace you can account for context and change all instances of a word/phrase differently.

  • Automatically replacing the words/phrases

We suggest using this method if you have little time or a lot of content to edit. Alternatively, if you want to uniformly change all instances of one word/phrase with a new one, automatic replacement will do it for you.

Start by selecting your database from the phpMyAdmin menu on the left and clicking on the SQL tab.

Before we start querying the database, let’s take a look at the form our query should take. The generic SQL query for an automatic replacement would look like this:

update table_name set field_name = replace(field_name, 'text you are searching for', 'text to replace it');

This query replaces all occurrences of the text you are searching for with text to replace it within the column called field_name located in a table called table_name.

We will give you two examples you can edit to make your own query.

Make sure to replace the text you are searching and text to replace it expressions with the actual word/phrase you need to change.

In the first example, we’re making changes to the wp_postmeta database table:

UPDATE wp_postmeta SET meta_value = replace(meta_value,'text you are searching for','text to replace it');

In the second example, we’re making changes to the wp_usermeta table:

UPDATE wp_usermeta SET meta_value = replace(meta_value, 'text you are searching for','text to replace it');

These queries will help you run automatic search and replace on your database. Please note that we put together generic queries and you must adapt them if you want them to work for you. So, when you choose which table you’ll be updating, make sure to add a column that actually exists within that table.

To illustrate, we’re going to make the following query:

update wp_posts set post_content = replace(post_content,'work','play');

Press Go to run the query. It will automatically replace every occurrence of the word work with the word play inside the post_content column in the wp_posts table.

Replace every occurrence of the word

Once you run your query, wait until you get a message confirming that it’s done. Depending on your database size it may take some time. The message you get will also list how many rows were affected by the change.

List how many rows were affected by the change

How to Add Code to The functions.php File for Search and Replace

Another method you can use to run search and replace on your website content involves creating custom functions. This means you need to be familiar with coding as you will be making changes to the code inside the functions.php file of your theme (or child theme). Please note, this method targets only specific parts of your website. Before proceeding, make a copy of your functions.php file as backup in case of coding errors.

To make functions-based changes to your website, you need to access the functions.php file of your theme or child theme. You can do this by connecting to your server using your FTP credentials and locating your root WordPress directory (public_html).

Adding code to the functions.php file

Navigate to the /wp-content/themes directory and choose your currently active theme (in our case, that’s Twenty Twenty). Find the functions.php file, right-click on it, and select View/Edit from the dropdown menu.

Functions.php view edit

Open the file using a text editor and scroll to the bottom. You will need to insert your custom function at the end of the file. We’ve added a comment // Your custom code goes here to mark the spot. Please note that the forward slashes are a way to mark a line of code that will not be executed in PHP, so make sure to leave them out when adding your code.

Your custom code goes here

We are going to show you an example of what a custom function looks like.

The following code replaces all occurrences of the word job with the word task. Using the is_singular() function and the_content hook will restrict this functionality to the content of your pages and blog posts, keeping the rest of your site unaffected.

function find_and_replace_words( $text ) {
if( is_singular( array( 'post', 'page' ) ) ){
$replace_words = array(
'job' => 'task',
);
$text = str_replace( array_keys( $replace_words ), $replace_words, $text );
}
return $text;
}
add_filter( 'the_content', 'find_and_replace_words' );

To adapt this code, just replace job and task with the word/phrase you want to change on your site.

Extra tip

You can use the same function to change multiple words or phrases on your site. This is done by adding new entries in the $replace_words array, in the same manner as with the first entry (‘job’ > ‘task’,). Moreover, you can change a range of things at the same time. For example, you can add links and replace words with the same custom function simultaneously. To illustrate, the following code will add an Instagram link to every occurence of the word Instagram on your posts and pages. At the same time, it will replace job with task.

function find_and_replace_words( $text ) {
if( is_singular( array( 'post', 'page' ) ) ){
$replace_words = array(
'Instagram' => '<a href="https://www.instagram.com/">Instagram</a>',
'job' => 'task',
);
$text = str_replace( array_keys( $replace_words ), $replace_words, $text );
}
return $text;
}
add_filter( 'the_content', 'find_and_replace_words' );

When you’re done, save the changes and upload the edited functions.php file back to the server to overwrite the old one. Doing this will apply the changes to your site content.

Now, the code above (and any variation of it you make) won’t alter the content of your database or the backend content of your page. It will only change the frontend display visible to users. Because of that, using this method to perform search and replace on your site can be restrictive. However, you can combine it with any of the other methods described in this article.

Final Thoughts

As we’ve seen, there are several ways to run find and replace on your site content. We hope you found one of them helpful and that you learned something new about WordPress tables, queries, and custom PHP functions along the way. Before you start to make changes on your own, keep in mind that performing search and replace can have serious effects if done improperly. Anything is possible—from crashing your site due to database errors to slowing down its speed with too much custom code. So you should make a backup and have a way to restore your site prepared, just in case.

Post your comment

Comments0