Creating config files for multiple environments in WordPress

In this article we're going to take a look at how to create a config file in WordPress that is compatible with multiple environments. The goal is to be able to deploy or upload all site files without worrying about breaking the code because you require different settings for different environments.

The problem

WordPress only has one configuration file by default titled 'wp-config.php' located in the root of your installation, let's take a quick look at what a section of this usually looks like:

/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'username');

/** MySQL database password */
define('DB_PASSWORD', 'password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

For one environment this is fine, but what if you want to work locally and / or deploy this code to a development or production server, then you're likely to need different settings (e.g. the database name or password). For this reason, usually you need to make sure you exclude this file each time you upload or deploy your code. 

Wouldn't it be nicer if the config file automatically detected which settings to use depending on the environment?

WordPress config variables

Before we look at an example let's just go over the different varaiables that we might want to change between environments:

  • DB_USER - Username to connect to the database
  • DB_PASSWORD - Password to connect to the database
  • DB_HOST - database host (usually localhost or IP)
  • DB_DEBUG - show development notice warnings
  • WP_HOME - web site url 
  • WP_SITEURL - location of wordpress files (usually the same as WP_HOME unless stored in a sub-folder)

Multi-environment config file

In the example below, we're going to create a config file that works for the following:

- Local - your local machine that may be running software such as WAMP or XAMMP, in this example the local environment has been set up with the '.dev' extension - yourdomain.dev

- Development - a test environment with the same configuration as live on a sub domain of 'dev.' 

- Production - the live site

To create a config file compataible with multiple environments, we're going to pick up the domain name that the site is being accessed on to define which settings should be used. This can be achieved using the $_SERVER global by accessing the 'SERVER_NAME' variable. Let's take a look at an example: 

switch($_SERVER['SERVER_NAME'])
{
    case 'yourdomain.dev':
        define('DB_NAME', 'wordpress');
        define('DB_USER', 'root');
        define('DB_PASSWORD', '');
        define('DB_HOST', 'localhost');
        define('WP_DEBUG', true);
    break;
   
    case 'dev.yourdomain.com':
        define('DB_NAME', 'wordpress');
        define('DB_USER', 'dev_username');
        define('DB_PASSWORD', 'dev_password');
        define('DB_HOST', 'localhost');
        define('WP_DEBUG', true);
    break;

    case 'yourdomain.com':
        define('DB_NAME', 'wordpress');
        define('DB_USER', 'live_user');
        define('DB_PASSWORD', 'live_password');
        define('DB_HOST', 'localhost');
    break;
}

Multiple domains and sub-domains

If you want to point multiple domains to the same site code or intend on using subdomains, you'll also need to include these in the list. To save duplicating settings you can just use a fallthrough like so:

case 'blog.yourdomain.com':
case 'yourdomain.com':
        define('DB_NAME', 'wordpress');
        define('DB_USER', 'live_user');
        define('DB_PASSWORD', 'live_password');
        define('DB_HOST', 'localhost');
    break;

...and that's all there is to it, you can now deploy the same code to any environment and it will still work. You can add additional environments (e.g. staging) by adding another case statement. Also notice how in the local and development environments we've added the additional 'WP_DEBUG' to show notices while developing. 

It's worth pointing out that this technique isn't exclusive to WordPress, by adapting the variables this same technique could be applied to other frameworks.

 

 

Sign Up

NEXT: Set up a local server with wordpress

In this post I'll be running you through all the steps you need to get you set up with a local server environment running with a copy of WordPress so you can play about with the code without worrying about who's looking at it or if you break it.

comments powered by Disqus
Sign Up

Popular Tags

350x250

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.