Web Design Blog

April 22, 2010

When I am working on a website I have a local development version that I program and test locally. When an update is implemented, it is tested locally before it is copied to the live server. WordPress sites are treated no differently. Here is how you can make the wp-config.php portable to work in your live and local environment. Simply replace the variables below with the actual information for your web site and you will be have a wp-config.php that can happily thrive in both environments.

I defined an additional constant ‘LIVE’ that I can refer to elsewhere in my code to determine if I am on the live server or not.

wp-config.php


if($_SERVER[‘HTTP_HOST’]==’livedomain.com’ || $_SERVER[‘HTTP_HOST’]==’www.livedomain.com’){

define(‘DB_NAME’, ‘db_name_live’); // The name of the database
define(‘DB_USER’, ‘mysql_username_live’); // Your MySQL username
define(‘DB_PASSWORD’, ‘mysql_password_live’); // …and password
define(‘DB_HOST’, ‘localhost’); // db host
define(‘DB_CHARSET’, ‘utf8’);
define(‘DB_COLLATE’, ”);
define(‘AUTH_KEY’, ‘put your unique phrase here’);
define(‘SECURE_AUTH_KEY’, ‘put your unique phrase here’);
define(‘LOGGED_IN_KEY’, ‘put your unique phrase here’);
define(‘LIVE’, true);

}else{

define(‘DB_NAME’, ‘name_of_db_local’); // The name of the database
define(‘DB_USER’, ‘mysql_username_local’); // Your MySQL username
define(‘DB_PASSWORD’, ‘mysql_password_local’); // …and password
define(‘DB_HOST’, ‘localhost’); // db host
define(‘DB_CHARSET’, ‘utf8’);
define(‘DB_COLLATE’, ”);
define(‘AUTH_KEY’, ‘put your unique phrase here’);
define(‘SECURE_AUTH_KEY’, ‘put your unique phrase here’);
define(‘LOGGED_IN_KEY’, ‘put your unique phrase here’);
define(‘LIVE’, false);

}

// You can have multiple installations in one database if you give each a unique prefix
$table_prefix = ‘wp_’; // Only numbers, letters, and underscores please!

// Change this to localize WordPress. A corresponding MO file for the
// chosen language must be installed to wp-content/languages.
// For example, install de.mo to wp-content/languages and set WPLANG to ‘de’
// to enable German language support.
define (‘WPLANG’, ”);

/* That’s all, stop editing! Happy blogging. */

if ( !defined(‘ABSPATH’) )
define(‘ABSPATH’, dirname(__FILE__) . ‘/’);
require_once(ABSPATH . ‘wp-settings.php’);
?>

MySQL Queries

Another thing to be conscious of when moving between a live and local server environment with WordPress are couple of records in the wp_options table of the database. The siteurl and liveurl records will be different in your local environment and live environment. If you copy your database from one to the other, just run the appropriate query below (with the liveurl and localurl variables changed to your actual data) depending on if you are copying from local to live or live to local.

Live to Local Query

Run query on local database.

UPDATE wp_options SET option_value=’http://localurl.com’ WHERE option_name=’siteurl’ ;

UPDATE wp_options SET option_value=’http://localurl.com’ WHERE option_name=’home’

Local to Live Query

Run query on live database.

UPDATE wp_options SET option_value=’http://liveurl.com‘ WHERE option_name=’siteurl’ ;

UPDATE wp_options SET option_value=’http://liveurl.com’ WHERE option_name=’home’

If you have any questions or suggestions, your comments are welcome below.

Comments

comments

Powered by Facebook Comments