Studio 24 WordPress Multi-Environment Config
This repository contains Studio 24's standard config setup for WordPress, which loads different config based on current environment. This allows you to have different site configuration (e.g. debug mode) for different environments (e.g. production and staging).
Credit is due to FocusLabs EE Master Config who gave me the inspiration for the organisation of the config files.
Please note the current version is v2, if you need to use the older v1 version please see the v1 release.
Contributing
WordPress Multi-Environment Config is an Open Source project. Find out more about how to contribute.
Security Issues
If you discover a security vulnerability within WordPress Multi-Environment Config, please follow our disclosure procedure.
How it works
The system detects what environment the current website is in and loads the relevant config file for that environment.
By default the environment is defined by the hostname, though you can also set this as an environment variable.
Config files are then loaded according to the current environment. There is support for loading a local config file for sensitive data, which is intended to not be committed to version control.
Config files
Up to three different config files are loaded:
- Default configuration (in
wp-config.default.php, e.g. shared settings such as$table_prefix) - Environment configuration (in
wp-config.{ENVIRONMENT}.php, e.g. any setting specific to the environment such as database name or debug mode) - Optional local settings (in
wp-config.local.php, e.g. any sensitive settings you do not want to commit to version control, e.g. database password)
Environment values
By default, environment values are:
production(live website)staging(test website for client review)development(local development copy of the website)
You can add other environment values by adding these to the wp-config.env.php file.
Setting the environment
The current environment is detected in one of three ways:
Environment variable
You can set an environment variable called WP_ENV to set which environment the website uses in your webserver configuration.
This is commonly done via Apache in your virtual host declaration:
SetEnv WP_ENV production
If you don't use Apache consult your webserver documentation.
Server hostname
The current environment can also be detected from matching the hostname with the domain setup in wp-config.env.php.
WP-CLI
If you're using WP-CLI you can specify your environment using an '.env' file.
You need to create a file called .env and simply write the current environment in this file as a string.
Example:
development
This file needs to be placed among the wp-config.*.php files (this applies if you keep these files in a sub-folder and the wp-config.php file in the web root).
It is recommended you do not add this file to version control.
The wp-config.env.php file
You need to edit the wp-config.env.php file to define some settings related to the current environment URL. This needs to
be set regardless of which method is used to set the environment, since all methods set the WordPress URL via settings
contained in this file.
This file contains a simple array, made up of:
environment names =>
domain => The domain name.
This can also be an array of multiple domains.
You can also use a wildcard * to indicate all sub-domains at a domain, which is useful when using
WordPress Multisite. If you use wildcards, set the domain should to a single string, not an array.
path => If WordPress is installed to a sub-folder set it here.
ssl => Whether SSL should be used on this domain. If set, this also sets FORCE_SSL_ADMIN to true.
Example usage:
$env = [
'production' => [
'domain' => 'domain.com',
'path' => '',
'ssl' => false,
],
'staging' => [
'domain' => 'staging.domain.com',
'path' => '',
'ssl' => false,
],
'development' => [
'domain' => 'domain.local',
'path' => '',
'ssl' => false,
],
];