{"id":28619610,"url":"https://github.com/wp-cli/wp-config-transformer","last_synced_at":"2025-06-12T04:40:41.101Z","repository":{"id":45075885,"uuid":"104569518","full_name":"wp-cli/wp-config-transformer","owner":"wp-cli","description":"Programmatically edit a wp-config.php file","archived":false,"fork":false,"pushed_at":"2025-05-13T09:08:10.000Z","size":186,"stargazers_count":83,"open_issues_count":1,"forks_count":26,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-05-13T10:23:55.702Z","etag":null,"topics":["config","hacktoberfest","transformer","wordpress","wp-cli"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wp-cli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-09-23T13:28:43.000Z","updated_at":"2025-05-13T09:08:12.000Z","dependencies_parsed_at":"2024-05-22T06:27:15.672Z","dependency_job_id":"a35275ca-e4fb-4728-9f3f-6374fd0a36fc","html_url":"https://github.com/wp-cli/wp-config-transformer","commit_stats":{"total_commits":142,"total_committers":14,"mean_commits":"10.142857142857142","dds":0.5704225352112676,"last_synced_commit":"c5b5349b86a3eea6c8a3f401f556f21a717aa80e"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/wp-cli/wp-config-transformer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-cli%2Fwp-config-transformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-cli%2Fwp-config-transformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-cli%2Fwp-config-transformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-cli%2Fwp-config-transformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wp-cli","download_url":"https://codeload.github.com/wp-cli/wp-config-transformer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wp-cli%2Fwp-config-transformer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259400408,"owners_count":22851807,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","hacktoberfest","transformer","wordpress","wp-cli"],"created_at":"2025-06-12T04:40:40.284Z","updated_at":"2025-06-12T04:40:41.057Z","avatar_url":"https://github.com/wp-cli.png","language":"PHP","readme":"# WP Config Transformer\n\nProgrammatically edit a `wp-config.php` file.\n\n[![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)\n\nQuick links: [Using](#using) \u0026#124; [Options](#options) \u0026#124; [How it works](#how-it-works) \u0026#124; [Testing](#testing)\n\n## Using\n\n### Instantiate\n\n```php\n$config_transformer = new WPConfigTransformer( '/path/to/wp-config.php' );\n```\n\n### Edit constants\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'WP_DEBUG', 'true', array( 'raw' =\u003e true ) );\n$config_transformer-\u003eadd( 'constant', 'MY_SPECIAL_CONFIG', 'foo' );\n$config_transformer-\u003eremove( 'constant', 'MY_SPECIAL_CONFIG' );\n```\n\n### Edit variables\n\n```php\n$config_transformer-\u003eupdate( 'variable', 'table_prefix', 'wp_custom_' );\n$config_transformer-\u003eadd( 'variable', 'my_special_global', 'foo' );\n$config_transformer-\u003eremove( 'variable', 'my_special_global' );\n```\n\n### Check for existence\n\n```php\nif ( $config_transformer-\u003eexists( 'constant', 'MY_SPECIAL_CONFIG' ) ) {\n\t// do stuff\n}\n\nif ( $config_transformer-\u003eexists( 'variable', 'my_special_global' ) ) {\n\t// do stuff\n}\n```\n\n## Options\n\nSpecial behaviors when adding or updating configs are available using the options array.\n\n### Normalization\n\nIn 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.\n\nLet's reconsider a poorly-formatted example:\n\n```php\n                 define   (    'WP_DEBUG'   ,\n    false, false     )\n;\n```\n\nThis time running:\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'WP_DEBUG', 'true', array( 'raw' =\u003e true, 'normalize' =\u003e true ) );\n```\n\nNow we will get an output of:\n\n```php\ndefine( 'WP_DEBUG', true );\n```\n\nNice!\n\n### Raw format\n\nSuppose you want to change your `ABSPATH` config _(gasp!)_. To do that, we can run:\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'ABSPATH', \"dirname( __FILE__ ) . '/somewhere/else/'\", array( 'raw' =\u003e true ) );\n```\n\nThe `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/'`.\n\n### Anchor string\n\nThe anchor string is the piece of text that additions will be anchored to.\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'anchor' =\u003e '/** Absolute path to the WordPress directory' ) ); // Default\n```\n\n### Anchor placement\n\nBy default, new configs will be placed before the anchor string.\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'placement' =\u003e 'before' ) ); // Default\n$config_transformer-\u003eupdate( 'constant', 'BAZ', 'qux', array( 'placement' =\u003e 'after' ) );\n```\n\n### Anchor separator\n\nBy default, the separator between a new config and its anchor string is an EOL (\"\\n\" on *nix and \"\\r\\n\" on Windows).\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'separator' =\u003e PHP_EOL . PHP_EOL ) ); // Default\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'separator' =\u003e PHP_EOL ) );\n```\n\n### Add if missing\n\nBy 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`.\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'add' =\u003e true ) ); // Default\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'add' =\u003e false ) );\n```\n\nIf the constant `FOO` exists, it will be updated in-place. And if not, the update will return `false`:\n\n```php\n$config_transformer-\u003eexists( 'constant', 'FOO' ); // Returns false\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'add' =\u003e false ) ); // Returns false\n```\n\n## How it works\n\n### Parsing configs\n\nConstants: https://regex101.com/r/6AeNGP/4\n\nVariables: https://regex101.com/r/cSLZZz/4\n\n### Editing in place\n\nDue 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.\n\nTo achieve this, the following steps are performed:\n\n1. A PHP block containing a config is split into distinct parts.\n2. Only the part containing the config value is targeted for replacement.\n3. The parts are reassembled with the new value in place.\n4. The old PHP block is replaced with the new PHP block.\n\nConsider the following horrifically-valid PHP block, that also happens to be using the optional (and rare) 3rd argument for constant case-sensitivity:\n\n```php\n                 define   (    'WP_DEBUG'   ,\n    false, false     )\n;\n```\n\nThe \"edit in place\" strategy means that running:\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'WP_DEBUG', 'true', array( 'raw' =\u003e true ) );\n```\n\nWill give us a result that safely changes _only_ the value, leaving the formatting and additional argument(s) unscathed:\n\n```php\n                 define   (    'WP_DEBUG'   ,\n    true, false     )\n;\n```\n\n### Option forwarding\n\nAny option supported by the `add()` method can also be passed through the `update()` method and forwarded along when the config does not exist.\n\nFor example, you want to update the `FOO` constant in-place if it exists, otherwise it should be added to a special location:\n\n```php\n$config_transformer-\u003eupdate( 'constant', 'FOO', 'bar', array( 'anchor' =\u003e '/** My special location' ) );\n```\n\nWhich has the same effect as the long-form logic:\n\n```php\nif ( $config_transformer-\u003eexists( 'constant', 'FOO' ) ) {\n    $config_transformer-\u003eupdate( 'constant', 'FOO', 'bar' );\n} else {\n    $config_transformer-\u003eadd( 'constant', 'FOO', 'bar', array( 'anchor' =\u003e '/** My special area' ) );\n}\n```\n\nOf course the exception to this is if you are using the `add =\u003e false` option, in which case the update will return `false` and no config will be added.\n\n### Known issues\n\n1. Regex will only match one config definition per line.\n\n**CORRECT**\n```php\ndefine( 'WP_DEBUG', true );\ndefine( 'WP_SCRIPT_DEBUG', true );\n$table_prefix = 'wp_';\n$my_var = 'foo';\n```\n\n**INCORRECT**\n```php\ndefine( 'WP_DEBUG', true ); define( 'WP_SCRIPT_DEBUG', true );\n$table_prefix = 'wp_'; $my_var = 'foo';\n```\n\n2. If the third argument in `define()` is used, it _must_ be a boolean.\n\n**CORRECT**\n```php\ndefine( 'WP_DEBUG', true, false );\ndefine( 'WP_DEBUG', true, FALSE );\ndefine( 'foo', true, true );\ndefine( 'foo', true, TRUE );\n```\n\n**INCORRECT**\n```php\ndefine( 'WP_DEBUG', true, 0 );\ndefine( 'WP_DEBUG', true, 'yes' );\ndefine( 'WP_DEBUG', true, 'this comma, will break everything' );\n```\n\n## Testing\n\n```bash\n$ composer global require phpunit/phpunit\n$ composer install\n$ phpunit\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwp-cli%2Fwp-config-transformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwp-cli%2Fwp-config-transformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwp-cli%2Fwp-config-transformer/lists"}