Download the PHP package roots/wp-config without Composer
On this page you can find all versions of the php package roots/wp-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download roots/wp-config
More information about roots/wp-config
Files in roots/wp-config
Package wp-config
Short Description Fluent configuration management for WordPress
License MIT
Informations about the package wp-config
wp-config
Fluent configuration management for WordPress
- Fluent API for clean, chainable configuration
- Built-in environment variable loading via
vlucas/phpdotenv - Conditional configuration with
when() - Instance-scoped hook system for extensible configuration
Support us
Roots is an independent open source org, supported only by developers like you. Your sponsorship funds WP Packages and the entire Roots ecosystem, and keeps them independent. Support us by purchasing Radicle or sponsoring us on GitHub — sponsors get access to our private Discord.
Requirements
- PHP >= 8.1
- Composer
Installation
Usage
Basic configuration
Conditional configuration
Accessing values
Environment variables
The Config class includes built-in support for loading environment variables:
Hook system
The Config class includes an instance-scoped hook system for extensible configuration:
Automatic before_apply hook
The Config class automatically executes any before_apply hooks when apply() is called. This enables packages to register configuration logic that runs automatically without requiring manual hook calls:
Upgrading from v1
Step 1: Update composer.json
Step 2: Replace static calls
Before:
After:
Step 3: Consolidate environment files
Before:
After:
Step 4: Update environment loading
Before:
After:
Step 5: Update hook usage
Hooks are now instance methods instead of static methods:
Before:
After:
Removed APIs
Config::remove()has been removed. Usewhen()blocks to conditionally set values instead.set()now overwrites previous values for the same key (useful inwhen()blocks for overriding defaults).
API reference
Config class
__construct(string $rootDir)
Creates a new Config instance with the specified root directory.
make(string $rootDir): static
Creates a new Config instance with a fluent-friendly named constructor.
bootstrapEnv(): self
Loads environment variables from .env files.
set(string|array $key, mixed $value = null): self
Sets a configuration value. Accepts a key/value pair or an associative array. Overwrites existing config map entries. Throws ConstantAlreadyDefinedException if a PHP constant with that name already exists.
env(string|array $key, mixed $default = null): self
Sets a configuration value from an environment variable. Falls back to $default if the variable is not set.
get(string $key, mixed $default = null): mixed
Gets a configuration value. Returns $default if the key is not set and a default is provided. Throws UndefinedConfigKeyException if the key is not set and no default is provided.
when(bool|Closure $condition, callable $callback): self
Conditionally executes configuration logic. The condition can be a boolean or a Closure that receives the Config instance.
apply(): void
Applies all configuration values by defining constants. Automatically runs before_apply hooks first.
addAction(string $tag, callable $callback, int $priority = 10): self
Adds a hook callback that can be executed later with doAction(). Returns $this for chaining.
doAction(string $tag, ...$args): self
Executes all callbacks registered for the specified hook. Returns $this for chaining.
Exceptions
ConstantAlreadyDefinedException: Thrown when attempting to redefine a constantUndefinedConfigKeyException: Thrown when accessing an undefined configuration key without a default
Full example
A complete Bedrock-style application.php configuration file:
Community
Keep track of development and community news.
- Join us on Discord by sponsoring us on GitHub
- Join us on Roots Discourse
- Follow @rootswp on Twitter
- Follow the Roots Blog
- Subscribe to the Roots Newsletter