wp-cli / entity-command

Manage WordPress comments, menus, options, posts, sites, terms, and users.
MIT License
100 stars 90 forks source link

Import/Export options as (pretty-printed) JSON #512

Open vadimkantorov opened 2 weeks ago

vadimkantorov commented 2 weeks ago

Currently we can import options, but have to iterate wp option update command invocation for each option.

A convenience command would be helpful which would import all options from a JSON file (e.g. produced with wp option list --format=json).

Currently wp option list --format=json does produce all options in JSON, but unfortunately it's not prettified in any way and dumped in one very-long line. So some prettification options would also be very helpful (and placing default options in the top of the file)


This is useful in setups where WordPress is used as a static-site-generator engine. For this usecase, it's nice to store all options in a plain JSON (or YAML) file and import them into a fresh-new WordPress installation during build.

swissspidy commented 2 weeks ago

The true powers of WP-CLI come to shine when combined with other shell commands and things like pipes.

This way you can easily call wp option update for a list of items, or pretty-print output using something like jq, i.e. wp option list --format=json | jq '.'. No need to reinvent the wheel :-)

vadimkantorov commented 2 weeks ago

wp option list --format=json | jq '.' - yes, that's what I'm using now, but it adds one more Ubuntu package dependency... I think, for the usecase of wp option list --format=json, prettified output should be the default, then it's immediately viewable and editable with any text editor without figuring out jq '.', and prettified output is usually natively supported by all underlying JSON serialization libraries (so likely only a matter of passing down the right switch to the JSON serialization function)...

wp export --filename_format=exportWXR.xml produces a prettified XML by default, so would be nice having the same functionality for options/JSON by default as well