Here’s a Quick Way to Search and Replace Your WordPress Database
Today, I’m going to let you in on a little trick that cuts my time in half when doing migrations. The handy tool that I use is a Search and Replace script. This can help search and replace your WordPress database when migrating from one domain to another. If you do not have a clue what I’m talking about, first download the Search and Replace Script for WordPress and read on to learn more about how to use this great tool.
What is this script?
First off, it’s main function is to do what it says, SEARCH and REPLACE something. This is similar to the functionality you use in your favorite text editor when needing to replace a word with something else. However, this does that same task but for your WordPress database.
Why do I need it?
As mentioned previously, the most common use for this script is when you migrate your site from one domain to another. Your domain in referenced all over your WordPress site within the database. For example, in the wp_options table under the siteurl and home option_name. Also, in posts that link to internal pages or images, your old domain will be in the image source or link href. So you will need a way to replace all those references to your old domain to your new domain.
Also, this script fixes any serialized data entries to your new domain name. Updating the serialized data will prevent missing widgets and settings being lost.
You’ve convinced me. Now, how do I use it?
Very simply actually. If you haven’t already, download the Search and Replace Script for WordPress. You should have downloaded a file name searchreplacedb2.php. If it is different then this, it may be because the file name has been changed since the writing of this article. Here is a step by step tutorial on how to change your domain name in your database.
- BACK UP CURRENT DATABASE! By the way, be sure to BACK UP CURRENT DATABASE!
- Upload the Search and Replace Script to the folder that your wp-config.php file is located.
- Go to http://yourdomain.com/searchreplacedb2.php.
- You should now see the homepage of the script. Make sure to have “Pre-populate the DB values form with the ones used in wp-config? It is possible to edit them later.” checked and press “Submit”.
- At the Database details page, you should see your database credentials pre-populated from your wp-config.php file. If not, fill in your database credentials now. Click “Submit DB details”.
- Now you can choose table to search and replace through. I normally do all tables for single WordPress installs but if you are on WPMU, then you may want to choose the correct site id tables to go through. Read the warning about the GUID columns in the posts table. If that is something you want to leave out, you have that option.
- This is where things get scary but luckily if something goes wrong, you already did a BACK UP OF YOUR CURRENT DATABASE! Now is the time to tell the script what to search for and what to replace it with. Once you tell the search and replace terms, click Submit Search String and let it do it’s thing. It’s normally pretty fast!
TIP: For domains, I recommend starting the search and replace string with an http://. For example, use http://yourdomain.com and not yourdomain.com. This will leave out email addresses in your database from being replaced. Don’t forget to put http:// in the replace box too!
- The script will now give you some stats on the changes it accomplished. Check your data and see if everything is working correctly and visit your site. Test Test Test is the key at this point!
- Finally, remember to remove the script from your server. This is a security measure so that no one can change your database through the script. Also, it will keep you off this list.
This is one of my favorite and most time saving tip I like to to use on all my migration that require some sort of search and replace. I hope this helps you in the future as well! IF you have found more uses or another tool that helps you with your search and replace tasks, I would love to hear about them in the comments.