How to automatically add some code to wp-config.php of new WP install?

Hi,

I am running Redis as Unix socket so for every new WP install I have manually added the code below to the file wp-config.php

define('WP_REDIS_SCHEME', 'unix');
define('WP_REDIS_PATH', '/var/run/redis/redis.sock');
define( 'WP_REDIS_PREFIX', 'mydomain.com' );
define('WP_REDIS_PASSWORD', 'ABbv8S68DDeYksD');

Is there any way to automatic this task when install a new WP site?

Thanks

Use this as a template by copying the current template and adding this code to that. Rename that template to your liking, and choose it in the UI for a site as a template.

1 Like

Are you installing a Redis instance per user or are different users/domains using a single Redis server install?

Multiple users are using a single Redis install on the same web server

I’m assuming you are using PHP Redis extension and not predis. Also are you not using W3 Total Cache to configure Redis from the dashboard without the need to do edit the wp-config file?

No. I used the plugin Redis object cache

You need to create your own installer

1 Like

yeah.. i figured that duplicating the WordPress installer file and modifying it to include the desired config will be the way to go forward. Here’s a suggestion from Google’s Gemini 2.5 Pro.

Please note this isn’t tested yet by me - it’s a suggestion by Gemini Pro 2.5.

You can create a new install app by following this documentation → Quick install app | Hestia Control Panel

We can easily adapt to this. The logic remains the same: we’ll add your custom code after wp-config.php has been created.


The Correct Steps for Your File

  1. File to Edit

    The file is indeed the one you’ve been looking at:

    /usr/local/hestia/web/src/app/WebApp/Installers/WordPress/WordPressSetup.php

  2. Backup the File (Important)

    If you haven’t already, please make a backup:

    Bash

    sudo cp /usr/local/hestia/web/src/app/WebApp/Installers/WordPress/WordPressSetup.php /usr/local/hestia/web/src/app/WebApp/Installers/WordPress/WordPressSetup.php.bak
    
    
  3. Add the Code

    Open the file for editing:

sh

sudo nano /usr/local/hestia/web/src/app/WebApp/Installers/WordPress/WordPressSetup.php

```

Go to the very end of the setupApplication function. Find the final closing curly brace `}` of that function, which is right before the final `}` of the class.

Add the following code **just before** that final closing brace:

```php
    // --- START: Custom Redis Configuration ---

    // Define the path to wp-config.php
    $wp_config_path = $target->getDocRoot() . '/wp-config.php';

    // Create the multi-line string for the config.
    // We get the domain from the $target object.
    $redis_config = <<<EOT

// Redis Object Cache Settings
define('WP_REDIS_SCHEME', 'unix');
define('WP_REDIS_PATH', '/var/run/redis/redis.sock');
define('WP_REDIS_PREFIX', '{$target->getDomain()->__toString()}');
define('WP_REDIS_PASSWORD', 'ABbv8S68DDeYksD');

EOT;

    // Append the configuration to the file
    file_put_contents($wp_config_path, $redis_config, FILE_APPEND);

    // Install and activate the Redis plugin using Hestia's wp-cli wrapper
    $this->appcontext->runWp($options['php_version'], [
        'plugin',
        'install',
        'redis-cache',
        '--activate',
        '--path=' . $target->getDocRoot(),
    ]);

    // --- END: Custom Redis Configuration ---
}

}

```

Your final few lines of the file should look like the snippet above. Notice you are adding the code just above the `}` that closes the `setupApplication` function.

What This New Code Does

This code is specifically tailored to the file you provided:

  • $target->getDocRoot(): It correctly gets the installation path from the $target object, which is available inside the function.

  • $target->getDomain()->__toString(): It dynamically gets the domain name for the new site and uses it for the Redis prefix.

  • file_put_contents(...): This remains the best way to append your settings to the newly created wp-config.php.

  • $this->appcontext->runWp(...): This uses Hestia’s built-in function to properly run the wp-cli command to install and activate the Redis plugin.

1 Like

Thank you for your time to find the solution. I will try it soon and post the feedback here.

Cheers,

1 Like