Ramin Hossaini

Migrating WordPress

Apr
18

Here are a couple steps I follow each time I move a WordPress instance from one environment to another. I’ll assume you’re moving from Development to Production.

Database configuration

A migration usually involves changing the database configuration in wp-config.php.

Instead of having to modify the file every time, you could just modify wp-config.php to include something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
if ($_SERVER['HTTP_HOST'] == 'dev') //could be 'localhost' or anything else
{
	define('DB_NAME', 'dev_db_name');
	define('DB_USER', 'dev_user');
	define('DB_PASSWORD', 'dev_password');
	define('DB_HOST', 'localhost');
	define('DB_CHARSET', 'utf8');
	define('DB_COLLATE', '');
}
else
{
	//production config:
	define('DB_NAME', 'prod_db_name');
	define('DB_USER', 'prod_user');
	define('DB_PASSWORD', 'prod_password');
	define('DB_HOST', 'prod_server');
	define('DB_CHARSET', 'utf8');
	define('DB_COLLATE', '');
}

That’s one less thing to worry about.

Database Export and Clean-up

I like using PHPMyAdmin for quickly exporting and importing between databases. But regardless of what tool you use, WordPress stores the full URL in the database (something I find very annoying) so you’ll also have to clean-up your database – these SQL commands have served me well:

1
2
3
4
5
6
7
8
9
10
11
--
-- run after doing an import
--
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://dev_host', 'http://www.prod_domain') 
WHERE option_name = 'home' OR option_name = 'siteurl';
 
UPDATE wp_posts SET guid = REPLACE(guid, 'http://dev_host','http://www.prod_domain');
 
UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://dev_host', 'http://www.prod_domain');
 
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://dev_host', 'http://www.prod_domain');

.htaccess

Depending on how your development environment differs from production, you might also have to change your .htaccess file. You could of course just edit the file, but I find the easiest for me is to:

  • Log into the WordPress admin (/wp-admin)
  • Expand the Settings tab, then click on Permalinks
  • Clicking on “Save changes” re-writes the .htaccess file

This is a tried and tested method I’ve been using a lot – but I’m curious to know how you do it. There might be some great plugins or tools that do all (or most) of this for you.

Leave a Reply

Your email address will not be published. Required fields are marked *