wp_get_environment_type() WordPress Function

The wp_get_environment_type() function is used to get the type of environment WordPress is currently running in.

wp_get_environment_type() #

Retrieves the current environment type.


Description

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

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


Top ↑

Return

(string) The current environment type.


Top ↑

More Information

  • This function allows plugin and theme authors to more easily differentiate how they handle specific functionality between production and development sites in a standardized way.
  • When development is returned by wp_get_environment_type(), WP_DEBUG will be set to true if it is not defined in the wp-config.php file of the site.
  • All hosts that support setting up staging environments are requested to set this feature to staging on those staging environments. Similarly, all developers with development environments shall set this value to development appropriately.

Example Usage:

switch ( wp_get_environment_type() ) {
    case 'local':
    case 'development':
        do_nothing();
        break;
     
    case 'staging':
        do_staging_thing();
        break;
     
    case 'production':
    default:
        do_production_thing();
        break;
}

Top ↑

Source

File: wp-includes/load.php

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

	if ( ! defined( 'WP_RUN_CORE_TESTS' ) && $current_env ) {
		return $current_env;
	}

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

	// Add a note about the deprecated WP_ENVIRONMENT_TYPES constant.
	if ( defined( 'WP_ENVIRONMENT_TYPES' ) && function_exists( '_deprecated_argument' ) ) {
		if ( function_exists( '__' ) ) {
			/* translators: %s: WP_ENVIRONMENT_TYPES */
			$message = sprintf( __( 'The %s constant is no longer supported.' ), 'WP_ENVIRONMENT_TYPES' );
		} else {
			$message = sprintf( 'The %s constant is no longer supported.', 'WP_ENVIRONMENT_TYPES' );
		}

		_deprecated_argument(
			'define()',
			'5.5.1',
			$message
		);
	}

	// 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
VersionDescription
5.5.1Removed the ability to alter the list of types.
5.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.