PHPackages                             wp-cli/wp-config-transformer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. wp-cli/wp-config-transformer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

wp-cli/wp-config-transformer
============================

Programmatically edit a wp-config.php file.

v1.4.5(2mo ago)859.7M↑39.8%26[1 issues](https://github.com/wp-cli/wp-config-transformer/issues)[1 PRs](https://github.com/wp-cli/wp-config-transformer/pulls)6MITPHPPHP &gt;=7.2.24CI passing

Since Jan 24Pushed 1mo ago8 watchersCompare

[ Source](https://github.com/wp-cli/wp-config-transformer)[ Packagist](https://packagist.org/packages/wp-cli/wp-config-transformer)[ Docs](https://github.com/wp-cli/wp-config-transformer)[ RSS](/packages/wp-cli-wp-config-transformer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (26)Used By (6)

WP Config Transformer
=====================

[](#wp-config-transformer)

Programmatically edit a `wp-config.php` file.

[![Testing](https://github.com/wp-cli/wp-config-transformer/actions/workflows/testing.yml/badge.svg)](https://github.com/wp-cli/wp-config-transformer/actions/workflows/testing.yml)

Quick links: [Using](#using) | [Options](#options) | [How it works](#how-it-works) | [Testing](#testing)

Using
-----

[](#using)

### Instantiate

[](#instantiate)

```
$config_transformer = new WPConfigTransformer( '/path/to/wp-config.php' );
```

### Edit constants

[](#edit-constants)

```
$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true ) );
$config_transformer->add( 'constant', 'MY_SPECIAL_CONFIG', 'foo' );
$config_transformer->remove( 'constant', 'MY_SPECIAL_CONFIG' );
```

### Edit variables

[](#edit-variables)

```
$config_transformer->update( 'variable', 'table_prefix', 'wp_custom_' );
$config_transformer->add( 'variable', 'my_special_global', 'foo' );
$config_transformer->remove( 'variable', 'my_special_global' );
```

### Check for existence

[](#check-for-existence)

```
if ( $config_transformer->exists( 'constant', 'MY_SPECIAL_CONFIG' ) ) {
	// do stuff
}

if ( $config_transformer->exists( 'variable', 'my_special_global' ) ) {
	// do stuff
}
```

Options
-------

[](#options)

Special behaviors when adding or updating configs are available using the options array.

### Normalization

[](#normalization)

In contrast to the "edit in place" strategy above, there is the option to normalize the output during a config update and effectively replace the existing syntax with output that adheres to WP Coding Standards.

Let's reconsider a poorly-formatted example:

```
                 define   (    'WP_DEBUG'   ,
    false, false     )
;
```

This time running:

```
$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true, 'normalize' => true ) );
```

Now we will get an output of:

```
define( 'WP_DEBUG', true );
```

Nice!

### Raw format

[](#raw-format)

Suppose you want to change your `ABSPATH` config *(gasp!)*. To do that, we can run:

```
$config_transformer->update( 'constant', 'ABSPATH', "dirname( __FILE__ ) . '/somewhere/else/'", array( 'raw' => true ) );
```

The `raw` option means that instead of placing the value inside the config as a string `"dirname( __FILE__ ) . '/somewhere/else/'"` it will become unquoted (and executable) syntax `dirname( __FILE__ ) . '/somewhere/else/'`.

### Anchor string

[](#anchor-string)

The anchor string is the piece of text that additions will be anchored to.

```
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'anchor' => '/** Absolute path to the WordPress directory' ) ); // Default
```

### Anchor placement

[](#anchor-placement)

By default, new configs will be placed before the anchor string.

```
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'placement' => 'before' ) ); // Default
$config_transformer->update( 'constant', 'BAZ', 'qux', array( 'placement' => 'after' ) );
```

### Anchor separator

[](#anchor-separator)

By default, the separator between a new config and its anchor string is an EOL ("\\n" on \*nix and "\\r\\n" on Windows).

```
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'separator' => PHP_EOL . PHP_EOL ) ); // Default
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'separator' => PHP_EOL ) );
```

### Add if missing

[](#add-if-missing)

By default, when attempting to update a config that doesn't exist, one will be added. This behavior can be overridden by specifying the `add` option and setting it to `false`.

```
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => true ) ); // Default
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => false ) );
```

If the constant `FOO` exists, it will be updated in-place. And if not, the update will return `false`:

```
$config_transformer->exists( 'constant', 'FOO' ); // Returns false
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'add' => false ) ); // Returns false
```

How it works
------------

[](#how-it-works)

### Parsing configs

[](#parsing-configs)

Constants:

Variables:

### Editing in place

[](#editing-in-place)

Due to the unsemantic nature of the `wp-config.php` file, and PHP's loose syntax in general, the WP Config Transformer takes an "edit in place" strategy in order to preserve the original formatting and whatever other obscurities may be taking place in the block. After all, we only care about transforming values, not constant or variable names.

To achieve this, the following steps are performed:

1. A PHP block containing a config is split into distinct parts.
2. Only the part containing the config value is targeted for replacement.
3. The parts are reassembled with the new value in place.
4. The old PHP block is replaced with the new PHP block.

Consider the following horrifically-valid PHP block, that also happens to be using the optional (and rare) 3rd argument for constant case-sensitivity:

```
                 define   (    'WP_DEBUG'   ,
    false, false     )
;
```

The "edit in place" strategy means that running:

```
$config_transformer->update( 'constant', 'WP_DEBUG', 'true', array( 'raw' => true ) );
```

Will give us a result that safely changes *only* the value, leaving the formatting and additional argument(s) unscathed:

```
                 define   (    'WP_DEBUG'   ,
    true, false     )
;
```

### Option forwarding

[](#option-forwarding)

Any option supported by the `add()` method can also be passed through the `update()` method and forwarded along when the config does not exist.

For example, you want to update the `FOO` constant in-place if it exists, otherwise it should be added to a special location:

```
$config_transformer->update( 'constant', 'FOO', 'bar', array( 'anchor' => '/** My special location' ) );
```

Which has the same effect as the long-form logic:

```
if ( $config_transformer->exists( 'constant', 'FOO' ) ) {
    $config_transformer->update( 'constant', 'FOO', 'bar' );
} else {
    $config_transformer->add( 'constant', 'FOO', 'bar', array( 'anchor' => '/** My special area' ) );
}
```

Of course the exception to this is if you are using the `add => false` option, in which case the update will return `false` and no config will be added.

### Known issues

[](#known-issues)

1. Regex will only match one config definition per line.

**CORRECT**

```
define( 'WP_DEBUG', true );
define( 'WP_SCRIPT_DEBUG', true );
$table_prefix = 'wp_';
$my_var = 'foo';
```

**INCORRECT**

```
define( 'WP_DEBUG', true ); define( 'WP_SCRIPT_DEBUG', true );
$table_prefix = 'wp_'; $my_var = 'foo';
```

2. If the third argument in `define()` is used, it *must* be a boolean.

**CORRECT**

```
define( 'WP_DEBUG', true, false );
define( 'WP_DEBUG', true, FALSE );
define( 'foo', true, true );
define( 'foo', true, TRUE );
```

**INCORRECT**

```
define( 'WP_DEBUG', true, 0 );
define( 'WP_DEBUG', true, 'yes' );
define( 'WP_DEBUG', true, 'this comma, will break everything' );
```

Testing
-------

[](#testing)

```
$ composer global require phpunit/phpunit
$ composer install
$ phpunit
```

###  Health Score

66

—

FairBetter than 99% of packages

Maintenance88

Actively maintained with recent releases

Popularity61

Solid adoption and visibility

Community33

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~129 days

Total

24

Last Release

60d ago

PHP version history (3 changes)v1.0.0PHP &gt;=5.3.29

v1.3.0PHP ^5.6 || ^7.0 || ^8.0

v1.4.3PHP &gt;=7.2.24

### Community

Maintainers

![](https://www.gravatar.com/avatar/6dde7f578e5530884238e7173f768ae3a890b6d66eb99262a82f2c494a1b67d4?d=identicon)[schlessera](/maintainers/schlessera)

---

Top Contributors

[![schlessera](https://avatars.githubusercontent.com/u/83631?v=4)](https://github.com/schlessera "schlessera (71 commits)")[![frankiejarrett](https://avatars.githubusercontent.com/u/522158?v=4)](https://github.com/frankiejarrett "frankiejarrett (61 commits)")[![swissspidy](https://avatars.githubusercontent.com/u/841956?v=4)](https://github.com/swissspidy "swissspidy (48 commits)")[![danielbachhuber](https://avatars.githubusercontent.com/u/36432?v=4)](https://github.com/danielbachhuber "danielbachhuber (15 commits)")[![andreamk](https://avatars.githubusercontent.com/u/12183989?v=4)](https://github.com/andreamk "andreamk (8 commits)")[![SteenSchutt](https://avatars.githubusercontent.com/u/6237359?v=4)](https://github.com/SteenSchutt "SteenSchutt (4 commits)")[![shreya0204](https://avatars.githubusercontent.com/u/78657883?v=4)](https://github.com/shreya0204 "shreya0204 (4 commits)")[![wojsmol](https://avatars.githubusercontent.com/u/4176111?v=4)](https://github.com/wojsmol "wojsmol (3 commits)")[![afragen](https://avatars.githubusercontent.com/u/1296790?v=4)](https://github.com/afragen "afragen (2 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (2 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (2 commits)")[![pwtyler](https://avatars.githubusercontent.com/u/8714062?v=4)](https://github.com/pwtyler "pwtyler (2 commits)")[![xknown](https://avatars.githubusercontent.com/u/1081870?v=4)](https://github.com/xknown "xknown (2 commits)")[![johnrom](https://avatars.githubusercontent.com/u/1881482?v=4)](https://github.com/johnrom "johnrom (1 commits)")[![GaryJones](https://avatars.githubusercontent.com/u/88371?v=4)](https://github.com/GaryJones "GaryJones (1 commits)")[![ernilambar](https://avatars.githubusercontent.com/u/2098823?v=4)](https://github.com/ernilambar "ernilambar (1 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![jrfnl](https://avatars.githubusercontent.com/u/663378?v=4)](https://github.com/jrfnl "jrfnl (1 commits)")

---

Tags

confighacktoberfesttransformerwordpresswp-cli

### Embed Badge

![Health badge](/badges/wp-cli-wp-config-transformer/health.svg)

```
[![Health](https://phpackages.com/badges/wp-cli-wp-config-transformer/health.svg)](https://phpackages.com/packages/wp-cli-wp-config-transformer)
```

###  Alternatives

[kevinchappell/form-builder

jQuery formBuilder

2.7k6.9k2](/packages/kevinchappell-form-builder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
