how to update serialized options programatically?

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

Leave a Comment