wp_get_environment_type()

Retrieves the current environment type.


Description Description

The type can be set via the WP_ENVIRONMENT_TYPE global system variable, or a constant of the same name.

Possible values include ‘development’, ‘staging’, ‘production’. If not set, the type defaults to ‘production’.


Top ↑

Return Return

(string) The current environment type.


Top ↑

Source Source

File: wp-includes/load.php

function wp_get_environment_type() {
	static $current_env = '';

	if ( $current_env ) {
		return $current_env;
	}

	$wp_environments = array(
		'development',
		'staging',
		'production',
	);

	// Check if the environment variable has been set, if `getenv` is available on the system.
	if ( function_exists( 'getenv' ) ) {
		$has_env = getenv( 'WP_ENVIRONMENT_TYPES' );
		if ( false !== $has_env ) {
			$wp_environments = explode( ',', $has_env );
		}
	}

	// Fetch the environment types from a constant, this overrides the global system variable.
	if ( defined( 'WP_ENVIRONMENT_TYPES' ) ) {
		$wp_environments = WP_ENVIRONMENT_TYPES;
	}

	// Check if the environment variable has been set, if `getenv` is available on the system.
	if ( function_exists( 'getenv' ) ) {
		$has_env = getenv( 'WP_ENVIRONMENT_TYPE' );
		if ( false !== $has_env ) {
			$current_env = $has_env;
		}
	}

	// Fetch the environment from a constant, this overrides the global system variable.
	if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) {
		$current_env = WP_ENVIRONMENT_TYPE;
	}

	// Make sure the environment is an allowed one, and not accidentally set to an invalid value.
	if ( ! in_array( $current_env, $wp_environments, true ) ) {
		$current_env = 'production';
	}

	return $current_env;
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.