函数文档

wp_get_environment_type()

💡 云策文档标注

概述

wp_get_environment_type() 函数用于获取当前 WordPress 环境类型,支持 'local'、'development'、'staging' 和 'production' 四种值。该函数通过全局系统变量 WP_ENVIRONMENT_TYPE 或同名常量设置,未设置时默认为 'production',为插件和主题开发者提供标准化的环境区分方式。

关键要点

  • 函数返回当前环境类型字符串,可能值为 'local'、'development'、'staging' 或 'production'。
  • 环境类型可通过 WP_ENVIRONMENT_TYPE 全局系统变量或常量设置,常量优先级高于系统变量。
  • 当返回 'development' 时,如果 wp-config.php 中未定义 WP_DEBUG,则自动将其设为 true。
  • 函数内部使用静态变量缓存结果,因此必须在首次调用前设置 WP_ENVIRONMENT_TYPE,否则定义的环境类型可能不被识别。
  • 建议主机在暂存环境中设置为 'staging',开发者在开发环境中设置为 'development'。
  • WP_ENVIRONMENT_TYPES 常量已弃用,不再支持。

代码示例

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;
}

注意事项

  • 环境类型值必须为预定义之一,否则将回退为 'production'。
  • 函数首次调用后结果被缓存,后续调用直接返回缓存值,避免重复计算。

📄 原文内容

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’.

Return

string The current environment type.

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;
}

Source

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;
}

Changelog

Version Description
5.5.1 Removed the ability to alter the list of types.
5.5.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    See case from rtCamp. They placed in mu-plugins/non-production.php.

    </pre>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-4923' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_environment_type%2F%3Freplytocom%3D4923%23feedback-editor-4923" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-5819" data-comment-id="5819" class="comment byuser comment-author-ludwig-c odd alt thread-odd thread-alt depth-1">
    			<article id="div-comment-5819" class="comment-body">
    
    							<a href="#comment-content-5819" class="screen-reader-text">Skip to note 6 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/ludwig-c/" rel="external nofollow" class="url">Ludwig C.</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/wp_get_environment_type/#comment-5819">
    							<time datetime="2022-05-09T13:24:48+00:00">
    							4 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="5c1cfce540" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="5819" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_environment_type%2F%23comment-5819"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="100% like this"><span class="screen-reader-text">Vote results for this note: </span>2</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="5819" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_environment_type%2F%23comment-5819"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-5819">
    				<p>Setting the environment type by <em>.htaccess</em> or Apache configuration</p>
    <p><code># Rules to set WP_ENVIRONMENT_TYPE based on hostname<br />
    RewriteCond %{HTTP_HOST} [.]?localhost$<br />
    RewriteRule .? - [E=WP_ENVIRONMENT_TYPE:local]<br />
    RewriteCond %{HTTP_HOST} ^staging.domain.com$<br />
    RewriteRule .? - [E=WP_ENVIRONMENT_TYPE:staging]<br />
    RewriteCond %{HTTP_HOST} ^www.domain.com$<br />
    RewriteRule .? - [E=WP_ENVIRONMENT_TYPE:production]</code></p>
    <p>Setting the environment type by Nginx configuration (best inside the php location)<br />
    <code>fastcgi_param WP_ENVIRONMENT_TYPE staging;</code></p>
    				</div><!-- .comment-content -->
    
    					<section id='feedback-5819' class='wporg-has-embedded-code feedback hide-if-js' data-comment-count='0'>
    </section><!-- .feedback -->
    <footer class='feedback-links wporg-dot-link-list' >
    <a role="button" class="feedback-login" href="https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_environment_type%2F%3Freplytocom%3D5819%23feedback-editor-5819" rel="nofollow">Log in to add feedback</a></footer>
    </article><!-- .comment-body -->
    </li>
    			<li id="comment-6360" data-comment-id="6360" class="comment byuser comment-author-ashkanahmadi even thread-even depth-1">
    			<article id="div-comment-6360" class="comment-body">
    
    							<a href="#comment-content-6360" class="screen-reader-text">Skip to note 7 content</a>
    				<header class="comment-meta">
    					<div class="comment-author vcard">
    						<span class="comment-author-attribution">
    						<a href="https://profiles.wordpress.org/ashkanahmadi/" rel="external nofollow" class="url">ashkanahmadi</a>						</span>
    						<a class="comment-date" href="https://developer.wordpress.org/reference/functions/wp_get_environment_type/#comment-6360">
    							<time datetime="2023-02-22T13:10:24+00:00">
    							3 years ago							</time>
    						</a>
    
    																													</div>
    					<div class="user-note-voting" data-nonce="fa2c8571de" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="6360" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_environment_type%2F%23comment-6360"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="100% like this"><span class="screen-reader-text">Vote results for this note: </span>1</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="6360" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_environment_type%2F%23comment-6360"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div>				</header>
    				<!-- .comment-metadata -->
    			
    				<div class="wporg-has-embedded-code comment-content" id="comment-content-6360">
    				<p>You can turn this into various functions to use specifically in certain environments:</p>
    <pre class="wp-block-code"><code lang="php" class="language-php ">function is_local_environment() {
       $env = wp_get_environment_type();
       return (defined('WP_ENVIRONMENT_TYPE') && $env === 'local');
    }

    And then use that in your code. Example:

    if ( is_local_environment() ) {
      // do something only in local environment
    }