Backup and restore a WordPress option with WP-CLI

Plugins, themes and WordPress core typically store any settings as a WP Option. You can navigate to your site’s /wp-admin/options.php to see all the options, although some of their values are large sets of serialized data hidden from view.

You can also see option values using WP-CLI’s option command:

> wp option get active_plugins
array (
  0 => 'query-monitor/query-monitor.php',
  1 => 'log-viewer/log-viewer.php',
  2 => 'wordpress-importer/wordpress-importer.php',

To get the option in a universal string format the --format=json flag can be used:

> wp option get active_plugins --format=json

And then that output can be stored in a file:

> wp option get active_plugins --format=json > active_plugins.txt
> cat active_plugins.txt

Now the file can be used to restore the option value back at any time:

> wp option update active_plugins --format=json < active_plugins.txt
Success: Updated 'active_plugins' option.