Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Adding/Updating Multiple Options at Once #230

Open
daleharrison opened this issue Jan 31, 2019 · 10 comments
Open

Allow Adding/Updating Multiple Options at Once #230

daleharrison opened this issue Jan 31, 2019 · 10 comments

Comments

@daleharrison
Copy link

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.

@swissspidy
Copy link
Member

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.

Can you perhaps elaborate on your use case?

@schlessera
Copy link
Member

There would actually be a benefit to this: you only load up WordPress once for updating multiple options in one go.

@daleharrison
Copy link
Author

@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.

@sagarnasit
Copy link
Contributor

sagarnasit commented 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
Copy link

@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
Copy link

+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
Copy link

groganz commented Jun 5, 2021

+1

2 similar comments
@jachinfrancke
Copy link

+1

@AlexP11223
Copy link

+1

@santiazpi
Copy link

santiazpi commented 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)

  1. 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
  1. 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";
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests