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.
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
{"disable_emojis":"1","remove_query_strings":"1","remove_jquery_migrate":"1","hide_wp_version":"1","remove_wlwmanifest_link":"1","remove_rsd_link":"1","remove_shortlink":"1","remove_feed_links":"1","disable_self_pingbacks":"1","disable_rest_api":"","remove_rest_api_links":"1","disable_dashicons":"1","disable_google_maps":"1","disable_password_strength_meter":"1","disable_comments":"1","remove_comment_urls":"1","disable_heartbeat":"allow_posts","heartbeat_frequency":"60","limit_post_revisions":"","autosave_interval":"300","login_url":"","disable_woocommerce_cart_fragmentation":"1","disable_woocommerce_status":"1","disable_woocommerce_widgets":"1","cdn":{"cdn_directories":"wp-content,wp-includes","cdn_exclusions":".php"}}
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.