Skip to content

ToolKit

Daniel M. Hendricks edited this page Jan 23, 2018 · 1 revision

Methods

Environment

Methods:

  • get_environment()
    • Returns value of WP_ENV, if defined (for example, in wp-config.php)
    • Returns value of environmental variable ENVIRONMENT, if defined. Supports phpdotenv.
    • Default return of "production" when no variables are set.
  • is_production() - Returns true if get_environment() == "production", else false.

Setting Environment/Environmental Variables:

For example, if you wanted to set the development environment to "development" or "staging," you could:

  1. In wp-config.php: define( 'WP_ENV', 'staging' );
  2. Create a .env file in your WordPress (or plugin/theme, if supported) root containing: ENVIRONMENT=staging. You may also set any other environmental variables in the .env file(s) as desired (reference).
  3. Define the environmental variable ENVIRONMENT in your OS.

The choices of the using "production," etc string values and .env files was chosen to be compatible with Sage and Roots conventions.

Order of Operations:

This order can be useful for testing. Although the WP_ENV constant always takes precedence over anything else, if absent and if supported by your plugin/theme, you could set ENVIRONMENT=production in your WordPress root .env but set ENVIRONMENT=development in your plugin/theme's .env.

This would allow you to test the site in production mode, however, keep the plugin/theme in development mode (which, as another example, you may have configured not to cache scripts/certain variables in non-production mode).

Clone this wiki locally