I want to automate updating plugin options. There are some things that I repeat a lot.
With wp-cli I know I can update simple options like this:
php wp-cli.phar option update blog_public 1
However, some plugin options save their options in a serialized string.
Example of serialized option_value in wp_options:
a:9:{s:4:"from";s:21:"xx@xxx.com";s:8:"fromname";s:51:"xxx";s:4:"host";s:13:"smtp.xx.com";s:10:"smtpsecure";s:3:"ssl";s:4:"port";s:3:"465";s:8:"smtpauth";s:3:"yes";s:8:"username";s:21:"xx@xxx.com";s:8:"password";s:13:"xxx";s:10:"deactivate";s:0:"";}
How to update those options?
WP-CLI is definitely the answer to this after the update to 1.4.0 which introduced the pluck and patch commands for accessing serialized data in WordPress.
The pluck command takes this format for grabbing serialized values
wp option pluck <key> <key-name>
For example in the active_plugins option you can grab first item
wp option pluck active_plugins 0
The patch command takes this format for inserting, updating or removing serialized values (the action)
wp option patch <action> <key> <key-name> <value>
Deleting the first active_plugin would look like this
wp option patch delete active_plugins 0
The same pluck and patch were also added for other commands like postmeta, you can now use WP-CLI to do some cool loops for updating WordPress serialized data programmatically