Skip to content

Write environment-specific code

The VIP Platform’s VIP_GO_APP_ENVIRONMENT constant and the WordPress function wp_get_environment_type() can both be used to customize environment-specific behavior in code.

For example, a conditional statement could be used to set a variable with a different value depending on if the code is running on a production, development, or local environment. The VIP Platform constant and the WordPress function each return different values but can be used in a similar way.

Using VIP_GO_APP_ENVIRONMENT

The VIP_GO_APP_ENVIRONMENT constant can be used to conditionally run code based on the environment type to which the code has been deployed.

At a minimum, every VIP application has a production environment type. The application might have additional environment types such as developstaging, or preprod.

The VIP_GO_APP_ENVIRONMENT constant is defined by the type value of an environment. The type value for an application’s VIP Platform environments can be retrieved with the VIP-CLI command: vip app [ID].

In this code example, VIP_GO_APP_ENVIRONMENT is used to restrict code to run only on the production environment:

if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) && VIP_GO_APP_ENVIRONMENT === 'production' ) {
    // Run this only on production
} 

In this code example, code is restricted from running on the production and preprod environments:

$disallowed_envs = array( 'production', 'preprod' );
if ( defined( 'VIP_GO_APP_ENVIRONMENT' ) && ! in_array( VIP_GO_APP_ENVIRONMENT, $disallowed_envs, true ) ) {
    // Run this code in all environments except prodution and preprod.
}

Using VIP_GO_APP_ENVIRONMENT in local environments

  • By default, VIP_GO_APP_ENVIRONMENT is already defined as local in a VIP Local Development Environment.
  • For other local development applications (e.g., VVV, Laravel) the VIP_GO_APP_ENVIRONMENT constant is not pre-defined. To use the constant, define it as local in wp-config.php (e.g., define( 'VIP_GO_APP_ENVIRONMENT', 'local' ); ).
  • Never define VIP_GO_APP_ENVIRONMENT in vip-config.php or anywhere in the application code. It is already defined by the VIP Platform. If the code is committed and deployed it will cause PHP warnings in the logs: Warning: Constant VIP_GO_APP_ENVIRONMENT already defined....

Using wp_get_environment_type()

The WordPress function wp_get_environment_type() can also be used to write environment-specific code in plugins or themes. wp_get_environment_type() can only be used after WordPress has fully loaded. As a result, it will not work as expected if added to vip-config.php, which loads before WordPress.

Unlike the value set in VIP_GO_APP_ENVIRONMENT, the output of wp_get_environment_type() is limited to four possible values: production, development, staging, and local. The VIP Platform sets the return value of wp_get_environment_type() based on the following mapping of common VIP Platform environment names:

VIP Platform environment typewp_get_environment_type() return value
productionproduction
develop,
development
development
local local
any other custom name
(preprod, staging, testing, uat, etc.)
staging

Additional VIP Platform constants

Additional constants set by the VIP Platform with per-environment values can be used to customize environment-specific behavior.

As a best practice, code should always check that a constant is defined before using it, for example: if ( defined( 'VIP_GO_APP_ID' ) ) { …

Note

These constants are not defined in a VIP Local Development Environment.

The constant name and its platform-set values are as follows:

  • VIP_GO_APP_BRANCH: The Git branch deploying to the environment (e.g., production, production-built, develop-built)
  • VIP_GO_APP_ID: The environment’s unique numeric ID (e.g. 1234)

Listing an application’s environments and values

Use the VIP-CLI command vip app [ID] to list the environments that belong to an application. The output from that command will also include the application ID, environment ID, name, type, and deploying branch for each listed environment.

Last updated: June 17, 2024

Relevant to

  • WordPress