Categories

Supporting multiple databases in wordpress

I recently had the need of making changes to my website but I didn’t want to have those changes live until I have tested in my local environment.

Downloading the remote files and running a local server is pretty trivial. Making a backup of the remote database can be accomplished using phpMyAdmin or you can use any WordPress backup plugin (I personally like WP-DB-Backup)

The part that is annoying (at least in my case), is that I used CPanel+Fantastico to automatically create an instance of WordPress in my server. The issue is that the database credentials (name, username, password) are generated with some weird values. In my local copy I have to modify the wp-config.php file to add the credentials to connect to my local database. The problem with this is that I always have to remember not to upload the wp-config.php to the live server to prevent taking my own site down.

I figured that I should probably modify the wp-config.php in a way that will work in my local environment as well as the remote server. Here is a quick way to support multiple databases in WordPress by modifying the wp-config.php:

if(preg_match('/localhost:8888/', $_SERVER["HTTP_HOST"])){ 
	// local environment
	define('DB_NAME', 'local_db');
	define('DB_USER', 'root');
	define('DB_PASSWORD', 'root');
	define('DB_HOST', 'localhost:8889');
}else{
	// remote environment
	define('DB_NAME', 'remote_db');
	define('DB_USER', 'remote_user_name');
	define('DB_PASSWORD', 'remote_user_password');
	define('DB_HOST', 'localhost');
}

3 comments to Supporting multiple databases in wordpress

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

IMPORTANT! To be able to proceed, you need to solve the following simple math (so I know that you are a human) :-)

What is 6 + 6 ?
Please leave these two fields as-is: