Bug Report
Describe the current, buggy behavior
Just had this happen on a project. I don't know how the anchor string got duplicated in wp-config, but, if the default anchor string /* That's all, stop editing! appears more than once in wp-config.php, wp config set (and wp config add) inserts the new define() at every occurrence of the anchor, resulting in the constant being defined multiple times. This triggers Constant XXX already defined notices.
Root cause is in WPConfigTransformer::add() in wp-cli/wp-config-transformer:
$contents = str_replace( $anchor, $new_src, $this->wp_config_src );
str_replace() replaces all occurrences of the anchor, not just the first.
Describe how other contributors can replicate this bug
- Take a standard
wp-config.php and duplicate the /* That's all, stop editing! Happy publishing. */ line so it appears twice (this happens in the wild via copy/paste edits, host provisioning scripts, or merged config fragments)
- Run:
- Inspect
wp-config.php: define( 'FOO', 'bar' ); now appears twice, once before each anchor occurrence
Describe what you would expect as the correct outcome
The constant is inserted exactly once, at the first occurrence of the anchor:
Let us know what environment you are running this on
OS: Darwin 25.6.0 Darwin Kernel Version 25.6.0: Fri Jul 31 19:19:08 PDT 2026; root:xnu-12377.161.14~5/RELEASE_ARM64_T6050 arm64
Shell: /bin/zsh
PHP binary: /Users/{me}/.studio/php-bin/8.4.24-studio-2/php
PHP version: 8.4.24
php.ini used: /Users/{me}/.studio/php-bin/8.4.24-studio-2/php.ini
MySQL binary: /opt/homebrew/bin/mysql
MySQL version: mysql Ver 8.0.42 for macos15.2 on arm64 (Homebrew)
SQL modes:
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: phar:///Applications/Studio.app/Contents/Resources/cli/wp-files/wp-cli/wp-cli.phar
WP-CLI packages dir:
WP-CLI cache dir: /Users/{me}/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.12.0
Provide a possible solution
Replace only the first anchor occurrence, e.g.:
$pos = strpos( $this->wp_config_src, $anchor );
$contents = substr_replace( $this->wp_config_src, $new_src, $pos, strlen( $anchor ) );
The anchor's existence is already validated via strpos() earlier in add(), so $pos is guaranteed to be found.
Provide additional context/Screenshots
Nothing more to add.
Bug Report
Describe the current, buggy behavior
Just had this happen on a project. I don't know how the anchor string got duplicated in wp-config, but, if the default anchor string
/* That's all, stop editing!appears more than once inwp-config.php,wp config set(andwp config add) inserts the newdefine()at every occurrence of the anchor, resulting in the constant being defined multiple times. This triggersConstant XXX already definednotices.Root cause is in
WPConfigTransformer::add()in wp-cli/wp-config-transformer:str_replace()replaces all occurrences of the anchor, not just the first.Describe how other contributors can replicate this bug
wp-config.phpand duplicate the/* That's all, stop editing! Happy publishing. */line so it appears twice (this happens in the wild via copy/paste edits, host provisioning scripts, or merged config fragments)wp config set FOO barwp-config.php:define( 'FOO', 'bar' );now appears twice, once before each anchor occurrenceDescribe what you would expect as the correct outcome
The constant is inserted exactly once, at the first occurrence of the anchor:
Let us know what environment you are running this on
Provide a possible solution
Replace only the first anchor occurrence, e.g.:
The anchor's existence is already validated via
strpos()earlier inadd(), so$posis guaranteed to be found.Provide additional context/Screenshots
Nothing more to add.