Thanks to visit codestin.com
Credit goes to github.com

Skip to content

tangrufus/wp-config

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

wp-config

Fluent configuration management for WordPress

$config = new Config($rootDir);
$config
    ->set('WP_DEBUG', true)
    ->set('WP_HOME', env('WP_HOME'))
    ->when($config->get('WP_ENV') === 'development', function($config) {
        $config
            ->set('SAVEQUERIES', true)
            ->set('SCRIPT_DEBUG', true);
    })
    ->apply();
  • πŸ”„ Fluent API for clean, chainable configuration
  • 🌍 Built-in environment variable loading
  • πŸ”€ Conditional configuration with when()
  • πŸ“¦ Zero dependencies (except vlucas/phpdotenv)

Requirements

  • PHP >= 8.1
  • Composer

Installation

composer require roots/wp-config:^2.0

Usage

Basic configuration

use Roots\WPConfig\Config;

$config = new Config($rootDir);
$config->bootstrapEnv();

$config
    ->set('WP_DEBUG', true)
    ->set('WP_HOME', 'https://example.com')
    ->apply();

Conditional configuration

$config
    ->when(env('WP_ENV') === 'development', function($config) {
        $config
            ->set('WP_DEBUG', true)
            ->set('SAVEQUERIES', true)
            ->set('SCRIPT_DEBUG', true);
    });

Accessing values

$home = $config->get('WP_HOME');

$config
    ->set('WP_SITEURL', $config->get('WP_HOME') . '/wp')
    ->apply();

Environment variables

The Config class includes built-in support for loading environment variables:

$config->bootstrapEnv(); // Loads .env and .env.local files

$config
    ->set('DB_NAME', env('DB_NAME'))
    ->set('DB_USER', env('DB_USER'))
    ->apply();

Upgrading from v1

Step 1: Update composer.json

{
  "require": {
    "roots/wp-config": "^2.0"
  }
}

Step 2: Replace static calls

Before:

Config::define('WP_DEBUG', true);
Config::define('WP_HOME', env('WP_HOME'));
Config::apply();

After:

$config = new Config($rootDir);
$config
    ->set('WP_DEBUG', true)
    ->set('WP_HOME', env('WP_HOME'))
    ->apply();

Step 3: Consolidate environment files

Before:

// config/environments/development.php
Config::define('WP_DEBUG', true);
Config::define('SAVEQUERIES', true);

// config/application.php
Config::define('WP_HOME', env('WP_HOME'));
Config::apply();

After:

$config
    ->set('WP_HOME', env('WP_HOME'))
    ->when($config->get('WP_ENV') === 'development', function($config) {
        $config
            ->set('WP_DEBUG', true)
            ->set('SAVEQUERIES', true);
    })
    ->apply();

Step 4: Update environment loading

Before:

$dotenv = Dotenv::createImmutable($rootDir);
$dotenv->load();

After:

$config = new Config($rootDir);
$config->bootstrapEnv();

API reference

Config class

__construct(string $rootDir)

Creates a new Config instance with the specified root directory.

bootstrapEnv(): self

Loads environment variables from .env files.

set(string $key, mixed $value): self

Sets a configuration value.

get(string $key): mixed

Gets a configuration value.

when($condition, callable $callback): self

Conditionally executes configuration logic.

apply(): void

Applies all configuration values by defining constants.

Exceptions

  • ConstantAlreadyDefinedException: Thrown when attempting to redefine a constant
  • UndefinedConfigKeyException: Thrown when accessing an undefined configuration key

About

Bedrock's failsafe wp-config

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%