wp-cli/wp-config-transformer
WP-CLI utility for safely reading and modifying WordPress wp-config.php values. Add, update, or remove constants, variables, and settings without manual editing, enabling reliable, scriptable configuration changes in automation and deployment workflows.
Installation Add the package via Composer in your Laravel project:
composer require wp-cli/wp-config-transformer
Basic Usage Initialize the transformer in a Laravel service or command:
use WP_CLI\Utils\get_config_transformer;
$transformer = get_config_transformer();
$config = $transformer->get_config();
First Use Case
Update a single wp-config.php setting (e.g., DB_NAME):
$transformer->set_config([
'DB_NAME' => 'new_database_name',
]);
$transformer->save_config();
WP_CLI\Utils\ConfigTransformer for core logic.filesystem facade to manage wp-config.php paths dynamically.Dynamic Configuration Updates
Use in Laravel migrations or deploy scripts to sync wp-config.php with environment variables:
$transformer->set_config([
'DB_HOST' => env('DB_HOST'),
'DB_USER' => env('DB_USER'),
'DB_PASSWORD' => env('DB_PASSWORD'),
]);
$transformer->save_config();
Multi-Environment Handling
Load different wp-config.php templates per environment (e.g., wp-config.staging.php):
$transformer = get_config_transformer(config_path('wp-config.staging.php'));
Seeding Defaults
Pre-populate wp-config.php during Laravel’s bootstrap/app.php or a custom service provider:
public function boot()
{
$transformer = get_config_transformer();
if (!$transformer->get_config('AUTH_KEY')) {
$transformer->set_config([
'AUTH_KEY' => Str::random(64),
'SECURE_AUTH_KEY' => Str::random(64),
// ... other salts
]);
$transformer->save_config();
}
}
Storage::disk('local')->exists() to check for wp-config.php before transforming.Validator to ensure config values meet expectations (e.g., DB_PASSWORD length).wp-config.php before modifying:
$backup = copy(config_path('wp-config.php'), config_path('wp-config.backup.php'));
// ... transformations ...
if ($backup) {
unlink(config_path('wp-config.backup.php'));
}
File Permissions
wp-config.php (typically 644).chmod 644 storage/wp-config.php (adjust path as needed).Path Resolution
wp-config.php in the current directory. Explicitly pass the full path:
$transformer = get_config_transformer(public_path('wp-config.php'));
Syntax Errors
wp-config.php (e.g., unclosed quotes) will break the transformer. Validate with:
php -l /path/to/wp-config.php
Caching
$transformer->clear_cache();
get_config() to inspect values before saving:
print_r($transformer->get_config());
\Log::info('Updated wp-config', ['changes' => $transformer->get_config()]);
Custom Config Keys
Extend the transformer to handle non-standard keys (e.g., Laravel’s APP_DEBUG):
$transformer->set_config([
'APP_DEBUG' => env('APP_DEBUG', false),
'WP_DEBUG' => env('WP_DEBUG', false),
]);
Pre/Post Hooks Add Laravel events to trigger before/after transformations:
event(new \App\Events\WpConfigUpdating($transformer->get_config()));
$transformer->save_config();
event(new \App\Events\WpConfigUpdated($transformer->get_config()));
WP-CLI Integration Chain with WP-CLI commands for advanced use cases:
wp config transform --path=/path/to/wp-config.php --set=DB_NAME=test_db
How can I help you explore Laravel packages today?