Site icon Kumar Gauraw

When You Need And How To Mass Find And Replace In WordPress

Have you been in a situation when you updated your WordPress or one of your WordPress plugins which now requires you to do a global find and replace in many published posts on your blog?

There are times when you are forced to think of an option to replace a particular word or a URL by a new one because may be you changed domains or because you needed to replace a hard coded affiliate link to a new link for any reason.

There are many such scenarios and I was in that situation just a few days ago when one of the plugins Shortcode Ultimate on my website came up with a new update that changed the name of the property “color” to “background”.

Now, I have used many shortcodes in many blog posts across hundreds of posts and I now need a way to do a global find and replace where I could find for the word “note color” to “note background”. It was frustrating when a plugin update caused this issue, but, to my serendipity, the task was easy. 

Similarly, when I decided to replace DISQUS by CommentLuv Premium on my blog, I realized I had links in each post that pointed to “disqus_thread”. I needed to change that to a different value after DISQUS was gone. Well, that also went well using the same method of global find and replace.

You will definitely need to perform a global find and replace when you are migrating a WordPress blog from one domain to another. When you change the domain, you need to replace every single reference to the old domain from your database with the new one. This feature comes very handy in such situations.

In this post, I am going to share with you three methods to get the job done. Based on your comfort level, you can choose the option that suits you the best.

Three Methods For Global Find And Replace In WordPress

There are three convenient ways to find a text and replace it with another across your entire WordPress website. Of course it only means a global find and replace in the MYSQL database tables behind your WordPress website or blog. I have tested all three of these methods and all of them do work like a charm.

Alright, now, let’s get into these methods right now!

[note background=”#DDDDDD”]Make sure you take a fresh backup of your MySQL database before performing any such search and replace operation using any of these three methods. Why? Because you want to be in a position to restore your database in case anything goes wrong in the process.[/note]

Method One – Search And Replace WordPress Plugin

Search And Replace is just another WordPress plugin that simply needs to be installed and activated and then it does, when it is asked to do, what it is supposed to do.

Once the plugin is installed, you can search in ID, post-content, GUID, title, excerpt, meta-data, comments, comment-author, comment-e-mail, comment-url, tags/categories and categories-description. It has a simple user interface from your WordPress admin area which can be accessed from Tools -> Search/Replace menu item.

Once invoked, the plugin presents you with the following screen where you can choose the tables you want to consider for the search/replace operation and then provide which word is supposed to be searched and what it is to be replaced with:

 

Method Two – Global Search And Replace PHP Script

This is my favorite way of doing a global search and replace at a massive level without logging into WordPress area of my website. I also like this script because it doesn’t require me to add a plugin to my WordPress installation.

This is an external script file (basically, a simple PHP script) which you can download and place in your WordPress home directory. That means, this PHP file should be placed in the same directory where your wp-config.php file is located.

You can download the script from this location. The developers of this script maintain the latest version and provide helpful information on this page. Once the file is in place, you will invoke this script using your web browser with following URL syntax:

http://www.YourWebsiteDomain/ScriptFileName.php

The script will show you a screen where it will ask your permission to pre-populate your MySQL database information by reading your wp-config.php file. The screens looks like this:

Make sure the checkbox is selected and press the submit button. Now, it will bring a screen where you will be asked which tables you want to include in this operation. You can choose ALL tables or you can choose only wp_posts if you have to only replace something within your post contents. Choose the tables and continue.

Finally, it will ask you for search text and the new text that will replace your search text. Provide that information and submit. The script will search and replace all occurrences of the searched term with the new term across all the tables you selected for this operation.

The script will provide you with the status message showing how many occurrences were replaced. And then, you are done!

Method Three – Search And Update MYSQL Table Using phpMyAdmin

To perform a global search and replace using this method, you need to have access to your cPanel account. If you are ready, log into cPanel and start your phpMyAdmin from there.

When you are inside phpMyAdmin, select your MySQL database table of interest and then perform a SQL update statement.

For example, I wanted to replace the word “#disqus_thread” with “#respond” in every blog post. All I did was, went to the table “wp_posts” using phpMyAdmin  and opened the SQL tab to execute following SQL update statement:

After writing the query all I had to do was hit the “GO” button on the screen to execute the query and the job was done!

Simple and easy. Know which table (for blog posts, your table will always be wp_posts and the field which stores the blog post is “post_content” as shown in the image above) and write your find and replace query which is actually an UPDATE statement as shown above.  Execute the query and you are done!

Do You Have Any Questions/Suggestions?

If you have any questions about any of these methods, please ask in the comments section and I promise, I will respond to you.

If you know another method which can be as useful as any of these, please use the comment section to share your suggestions and add value to this post.

Thank you kindly!

Exit mobile version