• Resolved Robert Gadon

    (@yogaman5020)


    @desrosj In a local development site ( Local by WPEngine ), I’ve been exploring the function wp_get_environment_type() added in WordPress v5.5 . The function returns a value of ‘local’.

    Stepping through and inspecting the conditional checks within the function, the return value appears to be set by a globally defined constant WP_ENVIRONMENT_TYPE. However, I can’t identify where that constant is set within WordPress. Looking at the local server information for my development site via phpinfo() did not return any information on the constant WP_ENVIRONMENT_TYPE.

    Reassigning the function in /wp-config.php to a different value (e.g. ‘development’ === wp_get_environment_type() ), or defining the constant ( e.g. define( ‘WP_ENVIRONMENT_TYPE’, ‘development’ ); ) in that file has no effect.

    Result: the conditional check to define WP_DEBUG as true in /wp-includes/default-constants.php L#77 never runs.

    Question 1: Where is the global constant WP_ENVIRONMENT_TYPE being set either in WordPress, or on my local development server ( nginx on Local by WPEngine )?

    Question 2: Is is possible to modify the value currently returned by wp_get_environment_type()? If so, where and how should this be done?

    • This topic was modified 4 years, 1 month ago by Robert Gadon.
    • This topic was modified 4 years, 1 month ago by Robert Gadon.
Viewing 6 replies - 1 through 6 (of 6 total)
  • This Make post refers to the line number of the code that checks for the setting. That link is to the old 5.5 code which checks for WP_ENVIRONMENT_TYPES first.
    The newer 5.5.1 code checks for an environment variable first, and then for a PHP constant, but it doesn’t set either one. The function itself uses a static variable to hold the value.

    1. The constant is not set in WordPress.
    2. You should be able to define the constant in wp-config.php with one of the valid values, or it will default to ‘production’.

    It looks like this information hasn’t been added to https://www.ads-software.com/support/article/editing-wp-config-php/ yet.

    Thread Starter Robert Gadon

    (@yogaman5020)

    @joyously Well, you answered my first question; the value of the global variable WP_ENVIRONMENT_TYPE is managed by PHP, and not WordPress. That explains the use of getenv( 'WP_ENVIRONMENT_TYPE' ) in the conditional check at wp-includes/load.php L#178-183.

    As to your second point:

    “You should be able to define the constant in wp-config.php with one of the valid values, or it will default to ‘production’.”

    I am unable to set a constant for WP_ENVIRONMENT_TYPE in /wp-config.php to modify the value of the global environment variable ( for example, change ‘local’ to ‘development’ ). The Query Monitor plugin returns a PHP Error Notice with the following message:

    “Constant WP_ENVIRONMENT_TYPE already defined”

    with reference to /wp-config.php and the line number where the define() function is passed in. Rather than simply a case of ‘Well, works for me’, I wonder if others have experienced this same issue.

    • This reply was modified 4 years, 1 month ago by Robert Gadon.
    • This reply was modified 4 years, 1 month ago by Robert Gadon.

    Well, you answered my first question; the value of the global variable WP_ENVIRONMENT_TYPE is managed by PHP, and not WordPress.

    Well, not really. PHP doesn’t manage the value, it only provides a function to get the value from the environment (OS). Your question was where is the global constant being set, and that is separate from the environment variable. WordPress does not set the PHP constant (you can search the code and see this is so). If you set it, WP will use the value though.
    The check for the values happens pretty early in the loading sequence, so no plugins are loaded yet to set the constant. This is why the original filters were removed.

    I just tried putting define('WP_ENVIRONMENT_TYPE', 'local'); into my wp-config.php file and it works just fine(shows on the Info page of Site Health). No errors.
    Did you by chance put yours at the end of the file, after wp-settings.php is loaded? Any changes have to be before the line with
    /* That's all, stop editing! Happy publishing. */

    Thread Starter Robert Gadon

    (@yogaman5020)

    @joyously I’m following everything you’re saying above.

    On my local dev site, the function wp_get_environment_type() returns the value ‘local’. As things stand now, I can live with that.

    However, I am unable to modify the value of the environmental variable in /wp-config.php, either adding define('WP_ENVIRONMENT_TYPE', 'development'); _before_

    if ( ! defined( 'ABSPATH' ) ) {

    or after

    require_once ABSPATH . 'wp-settings.php';. [The latter gets the ball rolling to load wp_get_environment_type(). ]

    I don’t know if my experience here is particular to my local dev setup ( Local by WPEngine ) or not. Earlier this year, I spun up an instance of WP using Docker. I’ll have to go back to that site and see if I can override the global environmental variable in /wp-config.php or not.

    Could also be that wp_get_environment_type() has only been out a short while, and the developers for Local haven’t addressed the inability to update the global environmental variable.

    You need to get it straight: by using a define you are setting the PHP constant, not the environment variable. I know this is confusing, but the WP environment just happens to have the same name as all environment variables (using ‘environment’). You can’t set an environment variable with a PHP define.
    You probably understand this, but the way you describe it is inaccurate.

    I am able to define mine to ‘development’ and see it in Site Health > Info with no errors.
    Your setup could very well have it being set somewhere else. You should grep the local setup to see if there’s something you missed.

    @yogaman5020 and others using Local (localwp.com) as your development setup and running into this problem:

    Local is using the PHP.ini auto_prepend_file setting to prepend a PHP file that sets the WP_ENVIRONMENT_TYPE constant.
    Hence, defining it in wp-config.php won’t have any effect on your Local setup.

    You’ll find the public feature request here:
    https://localwp.canny.io/feature-requests/p/return-local-for-wpgetenvironmenttype-in-wp-55

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to locate and modify the ‘WP_ENVIRONMENT_TYPE’ global system variable’ is closed to new replies.