NOTEPAD

WordPress & Git: Local, Staging & Production Database Connection

By Nick Burge
17 May

Ever wondered how to easily manage different database connections across local, staging and production environments? Well we’ve done exactly that!

The WordPress Part…

By using bits of this code and slowly perfecting our process through trail and error, We thought we would share our development process to help others who are attempting to integrate Git with their WordPress workflow.

Basically I’ve recoded the wp-config.php file to search for different database connections. The files that we need to edit/create include:

  • wp-config.php (replace database connection code to look for files)
  • local-config.php (stores local database connection)
  • staging-config.php (stores staging database connection)
  • production-config.php (stores production database connection)

First we need to edit the wp-config.php file by removing the following code…

Then add in this code in its place…

As you can see, the above code makes reference to ‘local-config.php’, ‘staging-config.php’ and ‘production-config.php’. So let’s create these now…

and do a similar thing for ‘staging-config.php’ and ‘production-config.php’.

The Git Part…

Using a .gitignore file, we can control certain files from not being pushed (uploaded). For example, for this code to work, we need to block the local-config.php from being deployed to the staging server, otherwise the local connection will be loaded.

But to make things even easier and completely avoid using .gitignore files, we use Beanstalk to easily manage and deploy our repositories. So in the deployment settings when we want to deploy to the staging server, the settings look like this…

git-wordpress-local-to-staging

And when we want to deploy to the production (live) server, simply add ‘local-config.php’ AND ‘staging-config.php’ which will cause the WordPress ‘config.php’ file to load the ‘production.php’. You can even set up your repository to automatically deploy to the staging server.

Pretty simple, right?!

One response to “WordPress & Git: Local, Staging & Production Database Connection”

  1. Nice write up Nick. Great to see the different ways people work. I use a modified version of the setup found here – http://abandon.ie/wordpress-configuration-for-multiple-environments/. Works great for me. I like the idea of adding Beanstalk to the workflow though.

Leave a Reply