VersionPress\Utils\WpConfigEditor::updateConfigVariable PHP Method

updateConfigVariable() public method

By default it saves string in single quotes. See $usePlainValue.
public updateConfigVariable ( $variableName, string | number | boolean $value, boolean $usePlainValue = false )
$variableName
$value string | number | boolean
$usePlainValue boolean The value is used as-is, without quoting.
    public function updateConfigVariable($variableName, $value, $usePlainValue = false)
    {
        // https://regex101.com/r/oO7gX7/5
        $variableRegex = "/^(\\\${$variableName}\\s*=\\s*).*(;\\s*)\$/m";
        $variableTemplate = "\${$variableName} = %s;\n";
        self::updateConfig($value, $variableRegex, $variableTemplate, $usePlainValue);
    }

Usage Example

    /**
     * @test
     */
    public function editorUpdatesOnlyDesiredValueInWpConfig()
    {
        file_put_contents($this->commonConfigPath, '<?php
// ** MySQL settings ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', \'vp01\');

$table_prefix = \'wp_\';


$my_variable = \'value\';
$test = \'value\';
/* That\'s all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined(\'ABSPATH\') )
    define(\'ABSPATH\', dirname(__FILE__) . \'/\');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . \'wp-settings.php\');
');
        $a = new WpConfigEditor($this->commonConfigPath, false);
        $a->updateConfigVariable('my_variable', 'another value');
        $expectedContent = '<?php
// ** MySQL settings ** //
/** The name of the database for WordPress */
define(\'DB_NAME\', \'vp01\');

$table_prefix = \'wp_\';


$my_variable = \'another value\';
$test = \'value\';
/* That\'s all, stop editing! Happy blogging. */

/** Absolute path to the WordPress directory. */
if ( !defined(\'ABSPATH\') )
    define(\'ABSPATH\', dirname(__FILE__) . \'/\');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . \'wp-settings.php\');
';
        $this->assertEquals($expectedContent, file_get_contents($this->commonConfigPath));
    }
All Usage Examples Of VersionPress\Utils\WpConfigEditor::updateConfigVariable