config-command icon indicating copy to clipboard operation
config-command copied to clipboard

Allow multiple config values to be set at once

Open westonruter opened this issue 7 months ago • 3 comments

Feature Request

Describe your use case and the problem you are facing

In the wordpress-develop repo, running npm run env:install does the following:

wp_cli( `config set WP_DEBUG ${process.env.LOCAL_WP_DEBUG} --raw --type=constant` );
wp_cli( `config set WP_DEBUG_LOG ${process.env.LOCAL_WP_DEBUG_LOG} --raw --type=constant` );
wp_cli( `config set WP_DEBUG_DISPLAY ${process.env.LOCAL_WP_DEBUG_DISPLAY} --raw --type=constant` );
wp_cli( `config set SCRIPT_DEBUG ${process.env.LOCAL_SCRIPT_DEBUG} --raw --type=constant` );
wp_cli( `config set WP_ENVIRONMENT_TYPE ${process.env.LOCAL_WP_ENVIRONMENT_TYPE} --type=constant` );
wp_cli( `config set WP_DEVELOPMENT_MODE ${process.env.LOCAL_WP_DEVELOPMENT_MODE} --type=constant` );

The multiple calls here result in a poor experience as each command is invoked in a separate process firing up Docker each time:

[+] Creating 2/2
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Generated 'wp-config.php' file.
[+] Creating 2/2
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Updated the constant 'WP_DEBUG' in the 'wp-config.php' file with the raw value 'true'.
[+] Creating 2/2
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Added the constant 'WP_DEBUG_LOG' to the 'wp-config.php' file with the raw value 'true'.
[+] Creating 2/2
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Added the constant 'WP_DEBUG_DISPLAY' to the 'wp-config.php' file with the raw value 'true'.
[+] Creating 2/2
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Added the constant 'SCRIPT_DEBUG' to the 'wp-config.php' file with the raw value 'true'.
[+] Creating 2/2
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Added the constant 'WP_ENVIRONMENT_TYPE' to the 'wp-config.php' file with the value 'local'.
[+] Creating 2/2
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Added the constant 'WP_DEVELOPMENT_MODE' to the 'wp-config.php' file with the value 'core'.
[+] Creating 2/2
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Added the constant 'FOO_BAR' to the 'wp-config.php' file with the value 'baz'.
[+] Creating 2/2
 ✔ Container wordpress-develop-mysql-1  Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
Success: Database reset.
[+] Creating 2/2
 ✔ Container wordpress-develop-php-1    Running                                                                                                                                                                                                                                                                                                                       0.0s 
 ✔ Container wordpress-develop-mysql-1  Running          

Describe the solution you'd like

I'd like to be able to set multiple config values in one command. This was also requested for wp option update in https://github.com/wp-cli/entity-command/issues/230.

Such batch setting of config values may make the most sense as using JSON as input, as it probably doesn't make sense to try to pass everything in one command as args.

An alternative to this: https://github.com/wp-cli/ideas/issues/180

westonruter avatar Jun 07 '25 00:06 westonruter

The multiple calls here result in a poor experience as each command is invoked in a separate process firing up Docker each time

That is...something 😬

Since this all happens unconditionally right after creating the config file:

https://github.com/WordPress/wordpress-develop/blob/f83e66389dbe3164ccf0fb2a1132064fb161b8e4/tools/local-env/scripts/install.js#L14

They should use the --extra-php option on the config create command itself to add these all in one go when the file is created (example). Something like:

wp_cli(`config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --extra-php <<PHP
define('WP_DEBUG', ${process.env.LOCAL_WP_DEBUG});
define('WP_DEBUG_LOG', ${process.env.LOCAL_WP_DEBUG_LOG});
define('WP_DEBUG_DISPLAY', ${process.env.LOCAL_WP_DEBUG_DISPLAY});
define('SCRIPT_DEBUG', ${process.env.LOCAL_SCRIPT_DEBUG});
define('WP_ENVIRONMENT_TYPE', '${process.env.LOCAL_WP_ENVIRONMENT_TYPE}');
define('WP_DEVELOPMENT_MODE', '${process.env.LOCAL_WP_DEVELOPMENT_MODE}');
PHP
`);

whatever js syntax makes it interpolate those properly. Then it would avoid all of those additional wp config set calls and provide an immediate fix to the particular slowness here.

mrsdizzie avatar Jun 07 '25 01:06 mrsdizzie

Yeah, that would work. Interpolation could be handled by just relying on the fact that booleans and strings serialize to JSON (generally) the same as PHP string literals:

wp_cli(`config create --dbname=wordpress_develop --dbuser=root --dbpass=password --dbhost=mysql --force --extra-php <<PHP
define('WP_DEBUG', ${JSON.stringify(process.env.LOCAL_WP_DEBUG)});
define('WP_DEBUG_LOG', ${JSON.stringify(process.env.LOCAL_WP_DEBUG_LOG)});
define('WP_DEBUG_DISPLAY', ${JSON.stringify(process.env.LOCAL_WP_DEBUG_DISPLAY)});
define('SCRIPT_DEBUG', ${JSON.stringify(process.env.LOCAL_SCRIPT_DEBUG)});
define('WP_ENVIRONMENT_TYPE', ${JSON.stringify(process.env.LOCAL_WP_ENVIRONMENT_TYPE)});
define('WP_DEVELOPMENT_MODE', ${JSON.stringify(process.env.LOCAL_WP_DEVELOPMENT_MODE)});
PHP
`);

westonruter avatar Jun 08 '25 05:06 westonruter

The need for this has been greatly lessened in the wordpress-develop environment after https://github.com/WordPress/wordpress-develop/pull/8969

westonruter avatar Jun 15 '25 01:06 westonruter