brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wp option pluck reads one value from deep inside a WordPress option

WordPress
~/tips/tip-wordpress-option-pluck-nested

wp option pluck my_plugin_settings api timeout

Prints a single nested value from a WordPress option without dumping the whole array.

Plugins like to store all their settings in one option, as a big serialized array. When I need a single number from it, dumping the whole thing is overkill, and WP-CLI has a command for exactly one value.

wp option pluck takes the option name and then the keys to follow inside the value.

wp option pluck my_plugin_settings api timeout

Here my_plugin_settings stands for an option that holds an array with an api key, which in turn holds timeout. The command prints that one value. The docs list --format with plaintext (the default), json and yaml, which helps when the plucked value is an array itself:

wp option pluck my_plugin_settings api --format=json

To see the structure first, print the whole option with wp option get my_plugin_settings --format=json and read the key path from there.

The same family has wp option patch for changing a nested value without rewriting the option by hand. It takes an action (insert, update or delete), the option name, the key path and the new value:

wp option patch update my_plugin_settings api timeout 30

Patch writes to the database. Take an export with wp db export before you try it on a live site.

The key path has to match the stored value exactly, so check the structure with the --format=json output first. A patch on a key that does not exist fails with an error, as the docs example for wp option patch shows.

Further reading: wp option pluck (WP-CLI handbook)

More WordPress tips wp user application-password create anna backup-script --porcelain define( 'MEDIA_TRASH', true ); wp cli check-update wp theme delete --all wp media image-size All WordPress tips (85) All topics

Link