Search code examples
bashwp-cli

How iterate over wp options list output using bash?


I want to create a script that adds WordPress options in a fresh WordPress installation using WP-CLI.

I ran this command wp option list --format=json | jq -c -M '.[]'

The output of the command like this:

{"option_name":"siteurl","option_value":"http://wp.test"}
{"option_name":"home","option_value":"http://wp.test"}

So how to iterate over each object and run add command and passing key and value as an argument like this wp option add $option_name $option_value?


Solution

  • One approach: Use jq to build the wp commands and execute them in your shell:

    source <(wp option list --format=json |
             jq -r '.[] | "wp option add \"\(.option_name)\" \"\(.option_value)\""')