markhadjar / wp-config
Define WordPress constants using environment variables.
Fund package maintenance!
v1.0.0
2026-05-05 02:41 UTC
Requires
- php: ^8.4
- markhadjar/env: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
README
Define WordPress constants using environment variables.
Requirements
PHP 8.4+
Installation
Install via Composer:
composer require markhadjar/wp-config
Usage
Define 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.