Last updated on December 15, 2009. Tags: MySQL database, search and replace
The phrase "search and replace" (also called "find and replace") refers to the capability of an editing software to replace some or all instances of a string of text with another string of text. In word documents, spreadsheets and text files, you can open this feature by pressing CTRL + H. A dialog box will then open with two input fields, one is for the string that you need to find and replace, and the other is for the string that will be substituted.
Search and replace is very useful as it makes editing much faster and more accurate. There are several different reasons to use the search and replace feature. One is for editing a document for another group of audience that might not be familiar with some of the words that you used in the original version. Another reason is that you might have misspelled a word or a name and consistently used the wrong spelling throughout the document thinking that it's the correct spelling.
This feature can be very useful in a WordPress-powered websites. One feature that will be useful is to perform find and replace to all posts and pages as if the entire blog or WordPress-powered website is just one big word document. Aside from the reasons mentioned in the previous paragraph, this feature will be particularly useful in the web for one thing: changing the domain of the website.
For the website to remain functional, all instances of the old domain must be replaced with the new domain. You can also use this to make the internal links consistent for URL canonization e.g., making all internal links either with or without www rather than having mixed versions of the URLs.
To perform search and replace on WordPress, one must do it on the MySQL database from where the Wordpress-powered website retrieve its data. You can do it in two ways, one is by using a plugin to edit the database from the dashboard and the other is by editing the database manually. In this article, I will discuss the method wherein we need to use a plugin. This is recommended for those who do not know much about the internal workings of WordPress.
First, download the search-and-replace plugin. Then, upload it either by using the Install Plugin (Dashboard » Plugins » Add New) in your WordPress dashboard or by uploading it at [root directory] » wp-content » plugins using your FTP manager.
Next, back-up the database of your WordPress-powered website. If you do not know which one is this database, open the wp-config.php file in the directory where you installed WordPress and see it there. It is in the first line that is not a comment at right of the string DB_NAME. You can back-up this database using either the back-up function that comes with your hosting account or by using the export database feature in PHPMyAdmin.

Then, activate the plugin in your dashboard. You will then see a "Search and Replace" link under the "Setting". Click on that link and then, you will be able to select from a group of data within your database (content, comment, excerpt, etc) on which you will apply search and replace. You can do search and replace in post content only, in excerpt only, in content and comments, or in all data in your website.
This plugin was first created by Mark Cunningham and developed in its current version by Frank Bueltge. While this plugin is very useful in selecting only parts of the database on which the find and replace will be performed, I do remember cases wherein it was not able to replace all the strings that I need to replace. I already forgot the exact details but it eventually caused me to stop using this plugin and rely more on manually editing the database.
This plugin is useful in changing domains without changing the hosting. However, you need coordination with your domain and hosting provider as to when the new domain will take effect. If the domain was changed before you can perform find and replace with this plugin, the links within the dashboard will not work and you won't be able to activate the plugin. If the search and replace was done prior to changing of the domain, you will not be able to do anything in your site from the time you did the search and replace to the time the new domain is activated. The safest way perhaps is to have a certain span of time, the amount of time you need to install and activate the plugin and do the search and replace, when the old and new domains are both active and pointing to your site.
If you're changing both domain and hosting, the manual editing of database (which I will discuss in my next post) is more useful because you will be exporting your database as SQL file anyway.
Despite its quirks and the risk of editing the database, in general, this plugin is very useful especially for those who are new to WordPress and to those who wish to perform search and replace only to a specific part of the database.
Posted by Greten on December 7, 2009 under WordPress plugins
Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
* Required. Your email will never be displayed in public.