WordPress Tip: How to change the domain name

Ed: This article is part of the 21 Days of WordPress Tips

While it’s nice to build a new site behind a coming soon page, in most cases when you are designing a new site you’re doing it while a current site already exists. There are several different ways to do this, but in most cases you’ll test it at a demo domain name and then, when you launch, you’ll need to switch everything over to the live domain name. Unfortunately, this is not as simple as it sounds. Below are a few of the common pitfalls and how you can get around them safely.

Caution: Some of my tips below use SQL statements that change a lot of database content at once. While I use them on a regular basis with no problems, I don’t guarantee that you won’t screw something up. Please backup your database and wp-content folder before doing anything. You have been warned.

1. Hard coded links in your theme This will not be a problem 99% of the time, however if you’re theme was built custom or you have made changes to it, there might be places where you hard coded a link (i.e. http://mysite.com) instead of using one of the WordPress functions to call it (i.e. get_option()).

If this is a problem, just go through and replace each of the hard coded links with:

<?php echo get_option('siteurl'); ?>

This will pull your URL from the database. Once these changes are implemented, you’ll never need to touch them again even when you change the domain of your site.

2. All the links use the demo domain name

WordPress stores the domain name of your site in the database table called wp_options. If you try to login to your site on the new server it will automatically redirect you to the old domain name that is stored in the database.

What you need to do is open up the phpmyadmin for your server and run this SQL query with the relevant pieces changed to your situation:

update wp_options set option_value = replace(option_value,'http://demo-url.com','http://live-url.com');

This will change all of the references in your wp_options table to the live URL. This will make it so you can now login to the Dashboard and all of your links using the function from #1 will work as well.

Tip: To combat hackers and make the WordPress installation more secure, your options table may not be named exactly “wp_options”. Be sure to check your database first to make sure you have the table name correct.

3. Broken permalinks

When you set your permalinks under the Settings -> Permalinks page of the WordPress dashboard, it changes settings in your .htaccess file in the root of your site. So when you are running your site under a different domain name, these will often no longer work.

Thankfully this is a very easy fix. Merely click the “Save Changes” button on the Permalinks page to reset and it’s done.

4. Post/page images and files no longer load

Assuming you added content to the site prior to launch, you probably uploaded some images (or other files) with the media uploader and added them to the post content. Unfortunately these are now hard coded to the demo domain name.

Thankfully, there is any easy fix that you’ve already had practice with in step #2 above.

Log back into your phpmyadmin and run this SQL statement:

update wp_posts set post_content = replace(post_content,'http://demo-url.com','http://live-url.com');

This will change any references to the demo domain name in your content, whether an image, link, PDF, etc., to the live domain name.

And you’re done

Your site should now run at the live domain name with no problems. However, since hosts and test environments vary greatly, you may run into a problem I don’t mention above or one of my tips may not work for you (i.e. your host doesn’t have phpmyadmin installed). If this is the case, leave a comment below and I’ll do my best to help you out.