Tonight, I presented at the Dayton WordPress Meetup on “Keeping WordPress under Version Control“.  My talk was focused on using Git as the version control system, however, these tips can be used with any version control system.

 Why Use Version Control

  1. Its better to keep track of your changes
  2. Allows easier collaboration during development
  3. Make updating sites easier

Cautions of wp-conf.php under version control

I talk about how keeping wp-conf.php in your .gitignore file is “optional”.  Here is the reason I say this.  When I develop, I want my changes to wp-conf.php to be transferred when I update my site.  This is why I always keep my wp-conf.php file in my repository.  There are come gotcha’s with doing this though.  One of these is the database information is going to be different between your local, testing, and production servers so how do you get one wp-conf.php file to work for all servers.  I have set up a little block of code for taking care of this.

[php]

// ============================
// = Custom Environment Setup =
// ============================

$environments = array(
‘local’ => ‘mysite.local’,
‘development’ => ‘dev.mycoolsite.com’,
‘production’ => ‘www.mycoolsite.com
);

$server_name = $_SERVER[‘SERVER_NAME’];

foreach($environments AS $key => $env){
if(strstr($server_name, $env)){
define(‘ENVIRONMENT’, $key);
break;
}
}

// If no environment is set default to production
if(!defined(‘ENVIRONMENT’)) define(‘ENVIRONMENT’, ‘development’);

switch(ENVIRONMENT){
case ‘local’:
define(‘DB_NAME’, ‘myCoolSite’);
define(‘DB_USER’, ‘root’);
define(‘DB_PASSWORD’, ‘root’);
define(‘DB_HOST’, ‘localhost’);
define(‘WP_DEBUG’, true);
$table_prefix = ‘wp_’;
break;

case ‘development’:
….
break;

case ‘production’:
….
break;
}

[/php]

Now, You might say something like, It is not a good idea to keep your database password and user in the repository, and, I would agree with you.  This is why I also suggest to people to create a file that is above the webroot in the file structure on your server that includes the switch statement.  This way you can then call:

[php]<br />require_once(‘../wpConfEnvSwitch.php’);<br />[/php]

Which would include the switch statement above.  Most hosting providers will allow the web user to traverse up the file tree into a ‘private’ folder for things like this.