WordPress Domain Change Script
Changing the domain name or URL of a WordPress site can be a very frustrating and time consuming task. I usually have three development environments that I’m constantly switching between and all of them have completely different domain names.
| Local Server | http://localhost:8888/myProject |
| Preview Server | http://myProject.mysite.com |
| Production Server | http://www.myProject.com |
Fed up with all the steps needed to change the domain I decided it would be worth my time to build a script that could streamline the entire migration process. The first revision of this script was dirty and insecure (to say the least) but it significantly reduced the amount of time spent moving sites between servers and changing their domains.
In a later version I added functionality that allowed the script to read the wp-config.php file and extract DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, and $table_prefix. In addition to this I added five corresponding form fields and auto-populated them with the data parsed from the WordPress config file. This further reduced the amount of time spent setting up and running the domain change script.
When I started working on this latest revision I decided to go in another direction by developing the script into more of a standalone web application. I added simple authentication; better error checking; a console area displaying Errors, Notices, and Actions; and a “session” timeout feature all wrapped in a new user interface.
I’ve also added a mandatory 5 second wait (sleep) during authentication in order to combat the threat of a brute-force attack. However, even with this countermeasure in place it’s very important this script gets removed from the server as soon as the migration process has been completed.
How to Use / Instructions
- Backup your WordPress database.
- Seriously, Back Up Your Database!
- Situation One: You have a new server where you intend to upload your existing WordPress site files.
- Export the current WordPress database data into a sql dump file.
- Create a MySQL database on the new server.
- Import the WordPress database dump file into the newly created database.
- Open up the wp-config.php file and set DB_HOST, DB_USER, DB_PASSWORD, and DB_NAME to the correct values for the new server.
- Upload the WordPress directory contents to the domain directory on the new server.
- Situation Two: The WordPress files and database locations have not changed — just the domain name.
- Skip to step 5.
- Open up wp-change-domain.php in a text editor and scroll down to the “CONFIG” section.
- Under “Authentication Password” replace the default password with a VERY secure password of your choice.
- Upload wp-change-domain.php to the root directory of the WordPress site.
- the root directory is where the wp-config.php is located.
- In a web browser go to: http://www.yourNewDomain.com/wp-change-domain.php
- Type in your password that you set in step 6 at the authentication prompt.
- You will now be presented with the domain changer form.
- The script will try and auto-detect all of the settings, but it’s up to you to confirm they are all correct.
- Take one last look at the settings to verify that they are correct… then click the “Submit!” button.
- Go to your site’s home page at the new domain — all should be working!
- Once the domain has been changed remove this script from the server!
License
This class and its code is released under the New-BSD License.
Requirements
- PHP >= 5.0, with the MySQLi Extension enabled.
- WordPress >= 2.8.0
Untested on previous versions.
Download
The source code is available to anyone at http://github.com/veloper/WordPress-Domain-Changer.
If you’ve found an issue, improvement, or bug with this script please Contact Me.
- Pingback: Migrating Your WordPress MU installation to a New Domain | T...
- Pingback: wp-popular.com » Blog Archive » WordPress Domain...
11 Comments
DUDE! That worked awesome. Thanks.
Fantastic, many thanks for writing this, has just saved me hours!
Your script rocks, its a shame it doesn’t support multisite though. Changing domains with multisite installs is a huge pain in the ass. To do it, this just needs to loop through each blog and run the script on each one
@Ron – Actually, I’ve been developing a multisite branch but it’s been in desperate need of beta testers and feedback.
You can find the MU branch here: https://github.com/veloper/WordPress-Domain-Changer/tree/wordpress_mu_support
Any and all feedback would be greatly appreciated!
I already changed the domain in the site options table but I was hoping to change the rest of the database. When I type in my password it takes me to a 404 page not found on my site and now i’m reading on wordpress that you’re not supposed to change the GUI is that right? I seem to recall them having instructions on a query you can run to search the different tables and replace one domain with another any advice would be appreciated would love to use your script so I can quickly change from staging domain name to production thanks!
@Jason – In your case I would revert the changes you’ve made in your database and then run my script. Make sure you follow the instructions above on how to use the script.
Just wanted to say thanks for this script, it worked wonderfully.
Thanks for the script but after using it, how will search engines behave to the site?
Will they gather all the info all over again?
Is it king of redirection or is a complete migration?@Mahdi – Well, since you’re changing the domain the site would need to be re-indexed by search engines. You could try configuring 301 redirects on the old domain, but I’m not sure what that would do for rankings — I guess that’s question for an SEO guru.
I designed the script to do a strait migration following the mantra of “do one thing, but do it well.”
I’m glad you got some use out of it

This was fantastic! thank you!
Awesome Script, thanks so much.
Comment On This Article...
- April 13, 2010
- Categories: Development
- Trackback: Link
2 Trackbacks/Pingbacks