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:"[email protected]";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:"[email protected]";s:8:"password";s:13:"xxx";s:10:"deactivate";s:0:"";}

How to update those options?


4 Answers 4


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

  • 4
    This should be the accepted answer. Jul 5, 2018 at 8:12
  • 2
    A few things that are not in the documentation: wp option patch update <key> <key-name> <value> fails if the key does not exist and updates only if it does exist. wp option patch insert <key> <key-name> <value> updates if the key exists and adds new if it doesn't exist. ... this may not be intuitive if you're used to update_post_meta where the word update means "update or add". Jun 9, 2021 at 19:06

Solved it myself, this is how to do it:

If you want to programatically update serialized options:

download wp-cli from wp-cli.org

Find out what is the "key" for the options you are using. In this example the key is "wp_smtp_options"

If you don't know the key, search in wp_options table and try to figure it out.

Example: select * from wp_options where option_name like '%smtp%'

Now that you know your key, use this command to save your configuration to json file:

php wp-cli.phar option get wp_smtp_options --format=json > my_saved_config.txt

Whenever you want that configuration to be restored use this command

php wp-cli.phar option update wp_smtp_options --format=json < my_saved_config.txt


  • works in WAMPSERVER64
  • works with unicode characters like ñ á é
  • you can have your json file with paddings and spaces, for legibility

It would be great not having to use an intermediate file for this purpose. Does anybody know how to do it?

  • 1
    Thanks - great work. If anyone is trying to write a script without requiring external files you can just put the json in the option update line in single quotes. See my implementation on line 61 here: gist.github.com/squarecandy/cfdead55ca1d84496044 Apr 15, 2016 at 1:30
  • see my answer for a "one liner"
    – Laurent
    Dec 21, 2016 at 5:05

Here is how I do it in a bash script:

wp option get wp_smtp_options --format=json | php -r '
$var = json_decode( fgets(STDIN) );
$var->from = "[email protected]";
$var->fromname = "me";
print json_encode($var);
' | wp option set wp_smtp_options --format=json

The WP-CLI command option does use WordPress' Options API to do its job. Given, e.g. with the subcommand update, a correct input, an array, you should be able to do this with WP-CLI. You should make use of the --format parameter here, to make sure you get the same, json works generally fine for the update subcommand. Note, the subcommand get should return you the option unserialized, because the Options API is used, which you then can save, modify and/or transfer/set-up on other/new installations.

  • This doesn't work: wp option get wp_smtp_options > savedconfiguration.txt wp option update wp_smtp_options < savedconfiguration.txt First command saves as array, but second doesn't accept an array. I guess I should serialize it first, do you know how to do it?
    – lalo
    Aug 19, 2015 at 18:07
  • Did you try leveraging the format parameter? @lalo edit: just seen you figured it out.. Aug 19, 2015 at 18:24
  • in wp-cli.org doesn't say what are available options for format parameter. I wonder if it could be possible without having to use an intermediate file like I wrote in my answer.
    – lalo
    Aug 21, 2015 at 15:40
  • You have to look at the source to get an complete overview over the format parameter, which differs from (sub-)command to (sub-)command sometimes. It surely is possible, for example by writing a bash script and storing the data into a variable. @lalo Aug 21, 2015 at 17:51
  • here is the source code, but don't understand where are --format available options: github.com/wp-cli/wp-cli/blob/master/php/commands/…
    – lalo
    Aug 22, 2015 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.