• Resolved Jacob Schwartz

    (@mightyturtle)


    Hello,

    I’m testing W3TC configurations and replicating my complete setup between different environments. However, there are certain configurations that I want to be different between environments – like memcache node URLs. Is there a way to override those very specific settings locally, maybe by putting a defined constant in wp-config.php?

    Thanks!

    Jacob

    https://wordpress.org/plugins/w3-total-cache/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Ashok

    (@bappidgreat)

    @jacob Schwartz

    I am not aware of any define, even I have just gone through the code and I don’t see any define that can overwrite the settings I am afraid. What you do is to edit the plugin code, if you want to do so I can try to help with.

    Have a good day!

    Cheers
    Ash

    Thread Starter Jacob Schwartz

    (@mightyturtle)

    Thanks Ash, but modifying the plugin (without the author’s support) might be a bad idea. I know this plugin doesn’t get updated all that often, but my changes would eventually be overridden with a plugin update.

    On the other hand, if the plugin author wants to chime in, a plugin modification might resolve the issue!

    I would create different master.php files (from wp-content/w3tc-config/master.php) for the different configurations, and copy them over to their corresponding installation.

    It’s not exactly the “defined constant” approach you were looking for, but it might help you in separating the local development environment settings from production ones.

    @charlesif is on the right track..i was going to suggest the same thing.

    Even if the plugin finally got updated your configuration settings (held in master.php) would carry over.

    But if you are really adamant about having constants you could technically use them freely as values for the desired keys (in master.php) and then define them in your wp-config.php file. If you go this approach just make sure those defined constants in wp-config are set anywhere before the require_once(ABSPATH . 'wp-settings.php'); line.

    for example:

    in your wp-config.php:

    define('PGCACHE_MEMCACHED_SERVER', array( 0 => '127.0.0.1:11211'));

    in w3tc-config/master.php line 94-96 would become:

    'pgcache.memcached.servers' => PGCACHE_MEMCACHED_SERVER,

    Note, in the above example this would work in php7 but not earlier since define’s can only work on scalar types pre-php7

    Thread Starter Jacob Schwartz

    (@mightyturtle)

    Thanks guys, that’s an interesting idea. If I go down the route of controlling master.php myself and testing for defined constant overrides, I could potentially manage a lot of W3TC config in git, which is appealing.

    Here’s an idea for how to extensibly achieve overrides in master.php:

    <?php
    
    /*
     * The regular W3TC config array, but assigned to
     * a variable instead of returned directly.
     */
    $conf = array(...); 
    
    /*
     * W3TC_CONF_OVERRIDES can be either an array
     * to be merged directly, or a stringified JSON
     * representation of same.
     */
    if(defined('W3TC_CONF_OVERRIDES')) {
      $conf_overrides = W3TC_CONF_OVERRIDES;
      if(is_string($conf_overrides)) {
        $conf_overrides = json_decode($conf_overrides, TRUE);
      }
      if(is_array($conf_overrides) && !empty($conf_overrides)) {
        $conf = array_merge($conf, $conf_overrides);
      }
    }
    return $conf;

    And then in wp-config.php:

    define('W3TC_CONF_OVERRIDES', json_encode(array(
      'version' => 'abc123', // you probably wouldn't actually want to override this :P
      'dbcache.memcached.servers' => array('local.memcache:11211')
    )));

    Note the JSON action, which allows defined constants to be loaded with structured data pre-PHP7.

    Of course it’d be best if the plugin author adopted this as a solution, but in the meantime I’ve got something to work with!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Override settings in wp-config?’ is closed to new replies.