VersionPress\Utils\WpConfigEditor::updateConfigConstant PHP Method

updateConfigConstant() public method

By default it saves string in single quotes. See $usePlainValue.
public updateConfigConstant ( $constantName, string | number | boolean $value, boolean $usePlainValue = false )
$constantName
$value string | number | boolean
$usePlainValue boolean The value is used as-is, without quoting.
    public function updateConfigConstant($constantName, $value, $usePlainValue = false)
    {
        // https://regex101.com/r/jE0eJ6/2
        $constantRegex = "/^(\\s*define\\s*\\(\\s*['\"]" . preg_quote($constantName, '/') . "['\"]\\s*,\\s*).*(\\s*\\)\\s*;\\s*)\$/m";
        $constantTemplate = "define('{$constantName}', %s);\n";
        self::updateConfig($value, $constantRegex, $constantTemplate, $usePlainValue);
    }

Usage Example

コード例 #1
0
    /**
     * @test
     */
    public function editorWorksWithoutDefiningAbspath()
    {
        file_put_contents($this->commonConfigPath, '<?php
define(\'DB_NAME\', \'vp01\');
$table_prefix = \'wp_\';

require_once(ABSPATH . \'wp-settings.php\');
');
        $a = new WpConfigEditor($this->commonConfigPath, false);
        $a->updateConfigConstant('TEST', 'value');
        $expectedContent = '<?php
define(\'DB_NAME\', \'vp01\');
$table_prefix = \'wp_\';

define(\'TEST\', \'value\');
require_once(ABSPATH . \'wp-settings.php\');
';
        $this->assertEquals($expectedContent, file_get_contents($this->commonConfigPath));
    }
All Usage Examples Of VersionPress\Utils\WpConfigEditor::updateConfigConstant