How to locate and modify the ‘WP_ENVIRONMENT_TYPE’ global system variable

Joy

(@joyously)

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://wordpress.org/support/article/editing-wp-config-php/ yet.

@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 5 hours, 28 minutes ago by Robert Gadon.
  • This reply was modified 5 hours, 27 minutes ago by Robert Gadon.

Joy

(@joyously)

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. */

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

Joy

(@joyously)

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.



Source link