Skip to content

How to setup Multi-Environments in WordPress

Last updated on February 10, 2018

In this post, I would like to show you how to configure multiple environments in WordPress. By using this technique you can deploy your WordPress application to development, staging, and production environments without having to edit your config files or update the site URLs in your database. The system detects what environment the current website is in and loads the relevant config file for that environment.

How to setup

You can achieve this in many ways, I will show you elegant and easily extensible version here.

First, you need to create environment specific config files in your root or any were in your server.We need to load this files based on the environment.

For example lets assume 3 environment called development, testing, production

Config files Ex: config-{environment}.php

  • Development: config-development.php
  • Testing: config-testing.php
  • Production: config-production.php

URLs may like this

  • Development: http://localhost
  • Testing: http://testing.website.com
  • Production: http://website.com
 'development.website.com',
  'testing'     => 'testing.website.com',
  'production'  => 'website.com'
);
 
 
$http_host = $_SERVER['HTTP_HOST'];
 
 
foreach($environments as $environment => $hostname) {
  if (stripos($http_host, $hostname) !== FALSE) {
    define('ENVIRONMENT', $environment);
    break;
  }
}
 
// If no environment is set default to production
if(!defined('ENVIRONMENT')) define('ENVIRONMENT', 'production');
 
// Location of environment-specific configuration
$wp_db_config = 'wp-config/wp-db-' . ENVIRONMENT . '.php';
 
// Check to see if the configuration file for the environment exists
if (file_exists(__DIR__ . '/' . $wp_db_config)) {
  require_once($wp_db_config);
} else {
  // Exit if configuration file does not exist
  exit('No database configuration found for this host');
}
// Absolute path to the WordPress directory.
if ( !defined('ABSPATH') )
  define('ABSPATH', dirname(__FILE__) . '/');
 
// Sets up WordPress vars and included files.
require_once(ABSPATH . 'wp-settings.php');

Edit $environments accordingly so they match your environments and their corresponding hostnames.

Then create a wp-config directory in the root of your project and create the following files inside:

Edit $environments accordingly so they match your environments and their corresponding hostnames.

Then create a wp-config directory in the root of your project and create the following files inside – wp-db-development.php, wp-db-testing.php, wp-db-production.php

Edit and add the following lines to each of your wp-db-*.php files:


For wp-db-production.php turn debugging off by defining WP_DEBUG to false

define('WP_DEBUG', false);

Benefits / Advantages

1. No need to edit Database configuration values.

2. you can maintain different/unique authentication keys and salts for each environment.

3. New environments can be added easily.

4. No need to change Error reporting settings manually. It should be turned on on development servers and off on staging and production.

5. we can block robots for development, staging environments.(duplicate content will hurt your clients SEO).

6. It will reduce the deployment time.

That’s it.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments