You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow Adding/Updating Multiple Options at Once #230
I was just looking at wp-cli/wp-cli#2972 and #220 (regarding deleting multiple options), but I haven't seen anyone requesting what I'm looking for.
I'm wondering if it'd be possible to do something like this:
$ wp option update blogname "Random blog name" blogdescription "Some random blog description"
I'm not sure if this is the way it would actually be implemented – perhaps the options would need to be specified in JSON format instead, or something similar.
Perhaps this is already possible in some other way…if so, I hope someone can point me in the right direction as I can't find any documentation on this.
It's certainly possible to add something like this. I just don't see any benefit in this. Even with JSON input it would be way less readable than just looping through an array in bash and pass the values to wp option update one after the other.
@schlessera Thanks for weighing in on this…your comment sums up my thinking perfectly.
@swissspidy The company I work for builds and manages WordPress multisite networks for some fairly large organizations. We use WP-CLI for setting up and maintaining these networks, and currently we are doing what you described – wrapping multiple calls to wp option update in a loop via various scripts.
We've noticed that it takes a long time to run these scripts, especially when we are spinning up a new multisite network preloaded with 15-20 sites and populating 30-40 options per site, or performing maintenance on a network of 400+ sites, for example.
If it could be possible to update multiple options in one shot, this would save us a great deal of time, and I can also see this being a benefit for the community at large.
Reacted by Pascal Birchler, Timur Kamaev and Sebastian Schröder
Identifying key and value for multiple options will be harder when so many options to update at a time but we can use JSON file to update multiple options. This way we can update every option defined in json file by loading WordPress instance for one time only. What do you think about this approach @schlessera?
@daleharrison@sagarnasit Just wanted to know how have you guys been handling this case? (if any other than looping the keys and values individually)
It's 2020 and I think wp cli still doesn't have a bulk update for wp options command.
TIA!
+1 vote for this. My use case is the same as @daleharrison's. When setting many options for many sites in a multisite network, the time to execute each WP-CLI command separately adds up.
In the meantime, here is something that is working for me.
When you are updating tens or hundreds of options, the difference is ridiculous, seconds vs minutes, probably exponentially so.
(Disclaimer: this solution could certainly have security issues, those considerations are out of my purview)
Create executable file in your PATH, for example: wp-options-update-from-json to send the JSON file with the options through wp eval-file
#!/bin/bash
#
# Emulate `wp option update` to update multiple values in one load
# by providing a JSON file with the options and running companion
# file: php-file-wp-options-update-from-json.php
# php file should be in the same folder as script
# Provided JSON file schema as `wp option list --format=json`
#
# required argument 1 is a JSON file as provided by `wp option list --format=json`
# optional argument 2 the destination multisite url as used in global wp-cli --url
# https://stackoverflow.com/a/246128
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
wp eval-file $SCRIPT_DIR/php-file-wp-options-update-from-json.php $1 $2
Add php-file-wp-options-update-from-json.php on the same folder:
<?php
/**
* Updates multiple wp options provided by JSON file
* as generated from `wp option list --format=json [--url=sub.domain.site]`
*
* $arg[0] path to JSON file
* $arg[1] (opt) url of the subsite in a multisite
**/
if($args[1]){
$blog_id = get_blog_id_from_url($args[1]);
if ($blog_id){
switch_to_blog($blog_id);
echo "switching to blog $blog_id - {$args[1]}";
} else {
echo "no site found for {$args[1]}";
return;
}
}
$json_file = $args[0];
$json_string = file_get_contents($json_file);
$wp_options = json_decode($json_string, true);
foreach($wp_options as $o) {
$current_option = get_option($o['option_name']);
if($current_option === $o['option_value']) {
echo "{$o['option_name']} did not change.\n";
} else {
$updated = update_option($o['option_name'], $o['option_value']);
echo "\n***UPDATED*** {$o['option_name']} = $updated\n";
echo "Original: {$o['option_value']} \n";
echo "Current: $current_option \n\n";
}
}
Activity
swissspidy commentedon Jan 31, 2019
It's certainly possible to add something like this. I just don't see any benefit in this. Even with JSON input it would be way less readable than just looping through an array in bash and pass the values to
wp option updateone after the other.Can you perhaps elaborate on your use case?
schlessera commentedon Feb 4, 2019
There would actually be a benefit to this: you only load up WordPress once for updating multiple options in one go.
daleharrison commentedon Feb 5, 2019
@schlessera Thanks for weighing in on this…your comment sums up my thinking perfectly.
@swissspidy The company I work for builds and manages WordPress multisite networks for some fairly large organizations. We use WP-CLI for setting up and maintaining these networks, and currently we are doing what you described – wrapping multiple calls to
wp option updatein a loop via various scripts.We've noticed that it takes a long time to run these scripts, especially when we are spinning up a new multisite network preloaded with 15-20 sites and populating 30-40 options per site, or performing maintenance on a network of 400+ sites, for example.
If it could be possible to update multiple options in one shot, this would save us a great deal of time, and I can also see this being a benefit for the community at large.
sagarnasit commentedon May 6, 2019
Identifying key and value for multiple options will be harder when so many options to update at a time but we can use JSON file to update multiple options. This way we can update every option defined in json file by loading WordPress instance for one time only. What do you think about this approach @schlessera?
GauravKanted commentedon Jul 16, 2020
@daleharrison @sagarnasit Just wanted to know how have you guys been handling this case? (if any other than looping the keys and values individually)
It's 2020 and I think wp cli still doesn't have a bulk update for wp options command.
TIA!
mwittmann commentedon Mar 25, 2021
+1 vote for this. My use case is the same as @daleharrison's. When setting many options for many sites in a multisite network, the time to execute each WP-CLI command separately adds up.
groganz commentedon Jun 5, 2021
+1
jachinfrancke commentedon Nov 28, 2022
+1
AlexP11223 commentedon May 2, 2023
+1
santiazpi commentedon Jun 1, 2023
+1
In the meantime, here is something that is working for me.
When you are updating tens or hundreds of options, the difference is ridiculous, seconds vs minutes, probably exponentially so.
(Disclaimer: this solution could certainly have security issues, those considerations are out of my purview)
wp-options-update-from-jsonto send the JSON file with the options throughwp eval-filephp-file-wp-options-update-from-json.phpon the same folder:westonruter commentedon Jun 7, 2025
Related for setting multiple
wp-config.phpconstants: wp-cli/config-command#199And for a more general batch endpoint where arbitrary commands could be combined in a single call to WP-CLI: wp-cli/ideas#180