Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Wp Config Transformer Laravel Package

wp-cli/wp-config-transformer

Programmatically edit wp-config.php with WP Config Transformer. Add, update, remove, or check constants and variables while preserving existing formatting, with optional raw PHP values, normalization, and anchor-based insertions for safe automated configuration changes.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Add the package via Composer in your Laravel project:

    composer require wp-cli/wp-config-transformer
    
  2. 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();
    
  3. 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();
    

Where to Look First


Implementation Patterns

Workflows

  1. 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();
    
  2. 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'));
    
  3. 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();
        }
    }
    

Integration Tips

  • Laravel Filesystem: Use Storage::disk('local')->exists() to check for wp-config.php before transforming.
  • Validation: Combine with Laravel’s Validator to ensure config values meet expectations (e.g., DB_PASSWORD length).
  • Rollback Safety: Wrap transformations in transactions or backup 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'));
    }
    

Gotchas and Tips

Pitfalls

  1. File Permissions

    • Ensure the Laravel app has write permissions for wp-config.php (typically 644).
    • Debug with: chmod 644 storage/wp-config.php (adjust path as needed).
  2. Path Resolution

    • The transformer defaults to wp-config.php in the current directory. Explicitly pass the full path:
      $transformer = get_config_transformer(public_path('wp-config.php'));
      
  3. Syntax Errors

    • Invalid PHP in wp-config.php (e.g., unclosed quotes) will break the transformer. Validate with:
      php -l /path/to/wp-config.php
      
  4. Caching

    • The transformer caches parsed config. Clear cache after manual edits:
      $transformer->clear_cache();
      
  5. Placement Anchor Behavior (v1.4.7+)

    • As of v1.4.7, the transformer only inserts at the first occurrence of the placement anchor (e.g., /* That's all, stop editing! Happy publishing. */). If your wp-config.php contains multiple such anchors, only the first will receive new config entries. Ensure your template has a single anchor or place it correctly.

Debugging

  • Dry Runs: Use get_config() to inspect values before saving:
    print_r($transformer->get_config());
    
  • Logging: Log transformations for auditing:
    \Log::info('Updated wp-config', ['changes' => $transformer->get_config()]);
    

Extension Points

  1. 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),
    ]);
    
  2. 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()));
    
  3. 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
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log