Migrating WordPress Sites

Moving a WordPress site, whether it’s to your local environment or to a new server, is a common task with some potentially confusing issues. Typically there are three migration scenarios, broken down in this post along with things to look out for.

The Two Parts of a WordPress Site

There are two parts to every WordPress site:

The Files

This is everything you can see when viewing your WordPress site folder, whether it’s on your server or your own local environment. This includes the WordPress core files, themes, and plugins.

The Database

This contains all your site content including users, posts, pages, taxonomies, and any additional settings.

Scenario 1: Moving a WordPress Site on the Same Server to a New URL

Say we have a site called site.com and we bought a new URL that we want to use instead called newsite.com. Now newsite.com is pointing to our server, but it keeps redirecting back to site.com – why?

The problem is that WordPress stores the URL it expects to be at (in this case still site.com) in the database. Even if we’re using a new URL to access the site (newsite.com) WordPress still thinks it should be using the old URL.

There are several methods to correct this laid out here: https://codex.wordpress.org/Changing_The_Site_URL

But the easiest is to either edit the wp-config.php file and add the following two lines (with the URL being your own new site URL):

define( 'WP_HOME', 'http://newsite.com' );
define( 'WP_SITEURL', 'http://newsite.com' );

Or if you can’t edit the wp-config.php file you can add these two lines to your theme’s functions.php file:

update_option( 'siteurl', 'http://newsite.com' );
update_option( 'home', 'http://newsite.com' );

Very nice! Now your site is loading at the new URL. But you may be seeing some issues with images not loading, or links still going to the old URL.

When creating content in WordPress it will use the full URL if you insert a link or an image. So anytime you linked to a post/page, or included an image, WordPress would use the full URL including site.com with each entry. Now that the URL has been updated these aren’t correct, but we don’t want to go through every page, post, and image to correct it.

Fixing Images and Links Still Pointing to the Old URL

So how do we go through and fix all these old URLs in the database?

The easiest way is to use a plugin such as Better Search Replace.

If you have some experience with databases you may be wondering: “Why don’t I just run a find/replace on the database directly for the old URL and replace it with the new one?” The one snag with manually doing a find/replace is that WordPress serializes some data before storing it. A manual find/replace would break any serialized data.

Scenario 2: Migrating the Whole WordPress Site to a New Server

If you’re going from your local environment to a staging or live server you’ll need to move both the files and the database. You could manually move the files using FTP, and move the database using PHPMyAdmin to export it from your local environment, and then import it to your new server, following Scenario 1 above to update the URLs.

Or you could use the Duplicator plugin.

The plugin will create two files:

  1. A .zip file containing all your site files, as well as your database
  2. An installer.php file

You’ll download both these files after the plugin has created them.

Then you’ll connect, using your FTP client, to your new server and upload both files where you would like your WordPress site to live.

After the files are uploaded you’ll open up your web browser and enter the URL for your new server to open up the installer.php file which may look something like: http://newsite.com/installer.php

After that you’ll follow the guided steps to complete the install. This method also takes care of migrating the URL (both on the site itself, as well as any references in the database) so you don’t have to worry about any issues from Scenario 1.

Scenario 3: Migrating Just the Database

If your servers are all set up and you just want to grab the content from another server, or push your content to another server, you’ll just need to move the database. But as mentioned in Scenario 1 you’ll want to update any references to the URL in the database or you’ll run into issue with images loading or links going to the right place.

The easiest way I’ve found to do this is with the WP Migrate DB Pro plugin. It allows you to push/pull from any server that the plugin is installed onto, and the developer version allows you to sync your media files as well. Well worth the price if you find yourself needing to grab or push content semi-regularly.

There is also a free option available: WP Migrate DB but you will need to import the database dump it creates yourself. It’s not difficult if your server has PHPMyAdmin (or other tools) available and a good walk through can be found here.

Both versions of WP Migrate DB also take care of updating any old URLs to the new one safely avoiding any broken links or images.

Questions or comments? Hit me up on Twitter @ractoon