Extracting Plugin Configurations with WP-CLI

When managing many WordPress websites for others, a common task is dealing with plugins. Often it’s not enough to be able to bulk install and activate a particular WordPress plugin but also have that plugin preloaded with a certain set of configurations. Today we’ll look at how we can use WP-CLI to extract plugin configurations from one website which can be used to apply to many websites.

Searching for relating options with WP-CLI

Let’s look at Perfmatters, a popular performance plugin, as an example. First, find the option you’d like to export using wp option list –search=”<slug>*”. For Perfmatters, we might try something like this: wp option list --search="perf*" which will result in the following.

Searching for options relating to a plugin

Next export the entry to share with other sites. This may need to be repeated if multiple complex options are found.

wp option get perfmatters_options --format=json

Now for importing, we can easily script that, something like this. In Bash the heredoc is used to help deal with data containing quotes.

read -r -d '' data << heredoc

wp option set perfmatters_options "$data" --format=json

From my hosting panel I would then simply select any number of sites I’d like and then run the script in bulk. Also, some options don’t need to be exported. For example, you can easily inject your Perfmatters license key like this wp option set perfmatters_edd_license_key <key>.

Every plugin handles license keys differently so injecting into the database might not be the best option as it will likely skip the license activation. In fact, Perfmatters has its own command for adding and activating keys.

wp perfmatters activate-license <key>

Anyways that’s a rough outline. Every plugin is going to store things differently so it takes a bit of trial and error until you get all of the pieces copied over. Also, plugin configurations change over time. It’s a good idea to check every once in a while for new configurations or maybe automate your exports. Once you’ve written the script, it’s done. Use and repeat.