Sometimes a website runs on different servers and each instance has its own database and configuration. For example, I have a local copy for testing (“extreme” data to test performance, fake user accounts, debuging mode …), a staging copy (limited access and some fictional “beautiful” data for presentations) and a production copy (normal access, real data, no error reporting).
Managing multiple wp-config.php
files is rather messy. There is a chance to overwite one by another on deploy or backup restoration etc. So I use a universal file with custom structure that switches configuration according to a current $_SERVER[SERVER_NAME]
.
I wonder if this method has issues/pitfalls? Are there any smarter alternatives?
Do you have projects with multiple environments?
What does your wp-config.php looks like?
P.S. I also define a constant ACCESS_TYPE
(should rename it probably) that tells my theme what kind of environment is used. It helps to override some normal theme behavior. For example, I don’t need maintenance mode on my local copy obviously. So I can switch off all related plugins by a conditional hook without changing any settings manually etc.
Here is an rough example of “switching”:
<?php
// ...
/**
* This file provides custom configurations for different environments/mirrors.
* It switches settings according to a current $_SERVER[SERVER_NAME].
*
* Default environment settings
*
* 'db_charset' - Database Charset to use in creating database tables.
* 'db_collate' - The Database Collate type. Don't change this if in doubt.
* 'db_host' - Database host name. Use 'localhost' if in doubt.
* 'db_name' - The name of the database for ClassicPress.
* 'db_user' - Database user name.
* 'db_password' - Database user password.
* 'db_prefix' - ClassicPress Database Table prefix.
* 'access_type' - Environment type (see below).
* 'wp_debug' - ClassicPress debugging mode.
*/
$default_environment = array(
'db_charset' => 'utf8',
'db_collate' => '',
'db_host' => 'localhost',
'db_name' => '',
'db_password' => '',
'db_prefix' => 'wp_',
'db_user' => '',
'access_type' => 'private',
'wp_debug' => false,
);
/** Custom environments settings */
$environments = array(
'example.test' => array(
'db_name' => 'local_database',
'db_password' => '',
'db_user' => 'root',
'access_type' => 'local',
'wp_debug' => true,
),
'example.stage' => array(
'db_name' => 'staging_database',
'db_password' => '',
'db_user' => 'staging_database_user',
'access_type' => 'private',
'wp_debug' => true,
),
'example.com' => array(
'db_name' => 'production_database',
'db_password' => 'Sup3r^S3#rEtPa55w',
'db_user' => 'production_database_user',
'access_type' => 'public',
'wp_debug' => false,
),
);
/** Switching environment */
$env = $_SERVER['SERVER_NAME'];
$config = $default_environment;
if ( ! empty( $env ) && array_key_exists( $env, $environments ) ) {
$config = array_merge( $default_environment, $environments[ $env ] );
}
define('DB_NAME', $config['db_name'] );
define('DB_USER', $config['db_user'] );
define('DB_PASSWORD', $config['db_password'] );
define('DB_HOST', $config['db_host'] );
define('DB_CHARSET', $config['db_charset'] );
define('DB_COLLATE', $config['db_collate'] );
define('WP_DEBUG', $config['wp_debug']);
$table_prefix = $config['db_prefix'];
/**
* For developers: Environment type.
*
* May be used by themes or plugins to switch some behavior without changing
* settings in DB (ignore maintenance mode for example).
* - "private" -- authorization is required to view the site,
* maintenance mode is always on.
* - "local" -- site is visible to anyone,
* maintenance mode is ignored.
* - "public" -- site is visible to anyone, but maintenance mode
* can be activated manually in admin settings.
*/
define('ACCESS_TYPE', $config['access_type']);
// ...