Define WordPress constants using environment variables.
PHP 8.4+
Install via Composer:
composer require markhadjar/wp-configDefine constants with explicit values:
$config = new \MarkHadjar\WpConfig\WpConfig();
$config
->set('DB_NAME', 'wordpress')
->set('DB_USER', 'root')
->set('DB_PASSWORD', 'password')
->set('DB_HOST', 'localhost');Define constants from environment variables using typed defaults:
$config = new \MarkHadjar\WpConfig\WpConfig();
$config
->env('DB_NAME', 'wordpress')
->env('DB_USER', 'root')
->env('DB_PASSWORD', 'password')
->env('DB_HOST', 'localhost');Apply all entries as constants:
$config->apply();Check whether an entry exists or read its value:
if ($config->has('DB_HOST')) {
$host = $config->get('DB_HOST');
}Note: Calling set() or env() with a key that is already a defined constant will throw a ConstantWasAlreadyDefined exception. Calling apply() will throw if a constant was defined externally with a different value between configuration and apply. Calling env() with a default value whose type is not bool, int, float, string, or null will throw a DefaultValueTypeWasNotSupported exception.