3 different ways to search and replace text in WordPress

WordPress’ search and replace text tools give you a quick and easy way to find and replace any text you want without having to make any changes to your site, and you don’t need to have a strong programming or technical background to do this. Text search and replacement tools will be the perfect solution for you if you want to replace some values ​​or texts on your site with new values, or you want to update your content or update keywords.

In this article, we will explain to you how to search and replace text in WordPress using one of the distinctive plugins, Better Search Replace, as well as using a SQL query and using some specialized code.

Why do we need search and replace text tools in WordPress?

Text search and replacement add-ons and tools help you search for specific text or part of text within your articles and pages or anywhere within your site, and replace it with other text with ease.

Additionally, the search and replace tools enable you to make quick fixes to WordPress sites.

This will save you the trouble of searching for the required text in every article or publication you have. It is also worth noting that these tools allow you to search for elements other than text within your site. For example, you can search and replace URLs or images in the WordPress database for your site. In addition to the above, the search and text replacement tools provide some other useful features, perhaps the most notable of which are: 

  • Find and replace specific texts
  • Find and replace URLs
  • Find and replace images
  • Update the keywords on your website and publications.
  • Support HTML search and replace
  • Move HTTP headers to HTTPS  
  • Find and replace specific dates by content type
  • Possibility of replacing domains

An important point to note is that when you perform a search and replace process on your site, you will not be able to go back to the old version, so it is best to make a backup copy of the site in case of any problems or errors during the search and replace process. 


Ways to find and replace text in WordPress

There are many ways you can search and replace text in WordPress. We will go into three main methods: specialized WordPress plugins, such as the Better Search Replace plugin, using code, and finally via an SQL query. We will explain each of the previous methods in detail.

1- Use the Better Search Replace add-on

This addition makes it possible to replace texts, links, etc. without the need to deal with the database or do it manually through code.

The add-on also gives you a feature called dry run, which serves as a safety valve that enables you to avoid any unwanted change in your site. Therefore, through this feature, you can put the texts or values ​​that you want to change and run this feature, and then the add-on will show you All changes will be made, and if you are satisfied with these changes you can disable the feature and simply apply the changes.

You can download the add-on by going to: Add-ons << Add New << Search for the add-on << Click on Install Now, then click on the option to activate the add-on, as shown in the image below.

To learn more about how to install plugins on WordPress, you can read the following article: How to install plugins on a WordPress website step by step .

Then, after completing the installation and activation of the plugin, you will find the plugin settings located in the Tools section within the WordPress control panel of your site.

We will now provide a practical example of how to find and replace text using the extension.

For example, we have this sentence present in one of the articles on the site (Page Speed ​​Insights) and we want to replace it with the sentence (Site speed measurement tool).

We notice here in the image above in one of the articles on the site the phrase (Page Speed ​​Insights), which we will do through the addition, is to search throughout the site and replace this sentence with the new sentence.

All you have to do:

  1. Type Page Speed ​​Insights in the Search For box
  2. Write the new text (website speed measurement tool) in the Replace With box
  3. Choose the table (we want to search articles here, so we choose the wp_posts table)
  4. Then uncheck the Run As Dry option for the changes to be implemented and the text replaced
  5. Then press Run

The steps will be similar to the following illustration:

After clicking on the “Run Search\Replace” option and applying the replacement process, a message will appear at the top stating the number of pages that were scanned and the sentences that were affected and to which the changes were applied. 

In order to ensure that the process is carried out correctly, you can preview the changes made by the extension by clicking on the “ Click here ” option included in the change completion message. In our example we see the following change:

We see in the image how the phrase (Page Speed ​​Insights) has been replaced with the phrase (site speed measurement tool) without the need to search for it within the articles and with the click of a button.

Do not forget when you turn on the “ Run as dry ” feature, you will be able to see the changes and the number of items that will be affected by the change. To implement the changes, you must cancel the feature and then press the “ Run ” option again.

2- Using the manual method (DB PHP Script)

If you don’t want to go through the process of installing and working with WordPress plugins, you can simply use the manual method to search and replace text in WordPress. To do this you must do the following:

  • First, you must go to the interconnectit website , in order to download the software code required for this process.
  • When you go to the previously mentioned site, you must write some information (name and email), and you must also choose Other or Nothing, to download the code for free.

Then click on the “ Submit to receive download link ” option and the site will send a link to download the code to your email.

Then you must go and check your email and download the link to the file containing the code from there, as indicated by the arrow in the image below.

The file will be downloaded to your device in the form of a compressed file, which you must decompress. The file name will be ( Search-Replace-DB-4.1.3 ).
You can also change the file name to whatever you want

The next step will be to upload the new file that we uploaded using FTP to the site’s files and place it inside the Public_html folder.

After that, in order to search for the element you want to replace on your site, you must go from your browser to the following link: https://website.com/Search-Replace-DB-4.1.3

Where domain.com: will be the name of your site.

Where Search-Replace-DB-4.1.3: represents the name of the file that we downloaded from the email and decompressed, then a new window will open for you, through which you can perform the search and replace process. Where you will need to enter some information such as:

  • The text you want to search for and the new text
  • Database name
  • Defining and selecting tables,for example:
  •  In the replace field, we put the name of the text you want to search for.
  •  In the with field, we put the name of the new text into it
  • Database name field, where we put the name of the required database
  • Host, Port field, we put the host name as well as the port address used
  • Tables field, where we put the name of the table you want to search within, or we can choose to search in all tables.
  • After that, we can choose Search and Repalce, to begin the search and replace process

After that, you can click on Do a save test run to preview the changes that will be made to confirm them. This is an option similar to the “dry run” option that we mentioned in the previous method, or you can search and replace directly by clicking on Search and Replace.

 

3- Search and replace using SQL query

You can also search and replace text in WordPress by manually entering the database and writing an SQL query to complete the replacement process.

SQL stands for (Structured Query Language), which is the language used if you want to make modifications to the database.

These changes include adding, deleting or modifying data. Therefore, here we want to search for a specific text within the database and change it to a new text, and therefore we need to use this query language to make the changes.

To do this, you must do the following:

  1. Log in to the hosting control panel (Cpanel).
  2. To learn more about how to access the hosting control panel, you should read the following article: How to use the cPanel control panel? A comprehensive explanation for beginners

Then log in to the phpMyAdmin database

Then, from within the database, you must choose the SQL tab to add the required query

Then you have to add the following query (code):

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘Text to search’, ‘Text to replace it with’);

For example, let’s implement a practical example:

We have a sentence (Welcome to WordPrss) as in the following images, and we want to replace it with a sentence (Welcome to my site).

As we explained above, we must enter cpanel, then SQL, and write the following code

update wpmk_posts set post_content = replace ( post_content , ‘Welcome to WordPrss’ , ‘Welcome to my site’ )

As in the picture, we write the code and click “Go” to execute the query

After executing the query, we will be shown how many rows were affected by the operation and how long the query took to complete.

To ensure the success of the process, you must return to the site and review the changes, which will be as in the picture.

Thus, we have learned that if you are looking for a way to search and replace text in WordPress, you should try the Search and Replace plugin, which is considered one of the easiest ways to do so. You can also do this manually by using code or by using a SQL query if you like to supervise every modification process on your site

Avatar photo
I am a young man who has been working in WordPress and e-marketing for 10 years. I would like to share my experience with you so that we can become professional in WordPress I will be happy to share the experience with you.