Skip to content

Backgrounds

How-to Guides

Technical References

Code Quality and Best Practices /

Write environment-specific code

Sometimes you may want to conditionally run code, or set configurations such as API keys or endpoints, depending on whether your application is currently running in production, a staging site, or a local development environment.

We provide the VIP_GO_APP_ENVIRONMENT constant, which is populated with the name of the environment. This allows you to conditionally load code on a particular environment, or to avoid a particular environment.

Here’s an example of preventing code from running on production.

$disallowed_debug_envs = array(
    'production',
    'preprod',
);
if ( ! in_array( VIP_GO_APP_ENVIRONMENT, $disallowed_debug_envs, true ) ) {
    error_log( 'Some debugging information can go here and will never run on production or pre-production' );
}

Alternatively, you can have code which only runs on production:

if ( 'production' === VIP_GO_APP_ENVIRONMENT ) {
    // This code only runs on production, perhaps 
    // configuration for a live service
} else {
    // This code runs everywhere except production
}

For your local development environment, you may wish to set the VIP_GO_APP_ENVIRONMENT constant in wp-config.php. If the VIP_GO_APP_ENVIRONMENT constant is not set explicitly, the value will default to false. The value of VIP_GO_APP_ENVIRONMENT matches that in the site domain and also the branch tracked by that environment. For example, the environment https://vipclientcom-develop.go-vip.net tracks the develop branch in git and has the environment name develop, therefore VIP_GO_APP_ENVIRONMENT contains the string develop.

Last updated: April 09, 2021