wp_get_environment_type

函式
wp_get_environment_type ( No parameters )
返回值
  • (string) The current environment type.
定義位置
相關方法
wp_get_mime_typeswp_get_ext_typesget_comment_typewp_get_nav_menu_itemswp_get_document_title
引入
5.5.0
棄用
-

wp_get_environment_type: 這個函式用來確定WordPress正在執行的環境型別。它可以返回四個可能的值中的一個。”production”,”staging”,”development”, or”local”。環境型別是通過檢查幾個環境變數的值來確定的,包括可以在wp-config.php檔案中定義的WP_ENVIRONMENT_TYPE常數。

檢索當前環境型別。

該型別可以通過`WP_ENVIRONMENT_TYPE`全域性系統變數,或同名常量來設定。

可能的值是’local’, ‘development’, ‘staging’, and ‘production’。如果沒有設定,該型別預設為”生產”。

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' ) && 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;
}

常見問題

FAQs
檢視更多 >