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:

bash
> 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:

bash
> wp option get active_plugins --format=json
["query-monitor\/query-monitor.php","log-viewer\/log-viewer.php","wordpress-importer\/wordpress-importer.php"]

And then that output can be stored in a file:

bash
> wp option get active_plugins --format=json > active_plugins.txt
> cat active_plugins.txt
["query-monitor\/query-monitor.php","log-viewer\/log-viewer.php","wordpress-importer\/wordpress-importer.php"]

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

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

Leave a comment

Your email address will not be published. Required fields are marked *