Download the PHP package wp-cli/wp-config-transformer without Composer
On this page you can find all versions of the php package wp-cli/wp-config-transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wp-cli/wp-config-transformer
More information about wp-cli/wp-config-transformer
Files in wp-cli/wp-config-transformer
Package wp-config-transformer
Short Description Programmatically edit a wp-config.php file.
License MIT
Homepage https://github.com/wp-cli/wp-config-transformer
Informations about the package wp-config-transformer
WP Config Transformer
Programmatically edit a wp-config.php
file.
Quick links: Testing
Using
Instantiate
Edit constants
Edit variables
Check for existence
Options
Special behaviors when adding or updating configs are available using the options array.
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:
This time running:
Now we will get an output of:
Nice!
Raw format
Suppose you want to change your ABSPATH
config (gasp!). To do that, we can run:
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
The anchor string is the piece of text that additions will be anchored to.
Anchor placement
By default, new configs will be placed before the anchor string.
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).
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
.
If the constant FOO
exists, it will be updated in-place. And if not, the update will return false
:
How it works
Parsing configs
Constants: https://regex101.com/r/6AeNGP/4
Variables: https://regex101.com/r/cSLZZz/4
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:
- A PHP block containing a config is split into distinct parts.
- Only the part containing the config value is targeted for replacement.
- The parts are reassembled with the new value in place.
- 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:
The "edit in place" strategy means that running:
Will give us a result that safely changes only the value, leaving the formatting and additional argument(s) unscathed:
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:
Which has the same effect as the long-form logic:
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
- Regex will only match one config definition per line.
CORRECT
INCORRECT
- If the third argument in
define()
is used, it must be a boolean.
CORRECT
INCORRECT