In the last two articles we looked at installing and configuring WP-CLI and using WP-CLI to install WordPress. We will now look at how to use the utility to manage settings.

Managing options

The option subcommand lets you view and change website options. Any option that is defined in the database’s options table can be retrieved and altered.

You can list all options using wp option list. This will output the option_name and option_value fields in much the same way as MySQL/MariaDB formats output. Or, you can look for a specific option name by using wp option get. For instance, you can get the value of the admin_email field:

$ wp option get admin_email

You can change values via the wp option update command. Here, we change the admin_email value:

$ wp option update admin_email
Success: Updated 'admin_email' option.

$ wp option get admin_email

Customising WordPress

If you regularly install WordPress you might find yourself making the same configuration changes time and again. For instance, by default non-registered users are allowed to leave comments on a WordPress site. To change that you need to navigate to Settings > Discussion and tick the checkbox with the label Users must be registered and logged in to comment. With WP-CLI you can quickly change settings like these.

Some of the default comment settings in WordPress.

Image: the default comment settings in WordPress. If you don’t like comments there is a lot to tweak, which is where WP-CLI comes in handy.

The main trick is finding out which setting in the WordPress dashboard corresponds to which settings in the options table. Many of the options are listed on the Option reference page in the WordPress Codex. For instance, the page shows that the “Users must be registered and logged in to comment” option is named comment_registration and that possible values are 0 (false) and 1 (true).

Once you know the name of an option you can easily change the value:

$ wp option get comment_registration

$ wp option update comment_registration 1
Success: Updated 'comment_registration' option.

Similarly, you can disable comments on all new posts by changing the value of default_comment_status to “closed”:

$ wp option get default_comment_status

$ wp option update default_comment_status closed
Success: Updated 'default_comment_status' option.

The “Attempt to notify any blogs linked to from the post” option is named default_pingback_flag:

$ wp option get default_pingback_flag

$ wp option update default_pingback_flag 0
Success: Updated 'default_pingback_flag' option.

And to give one more example, the “Allow link notifications from other blogs on new posts” option is named default_ping_status.

$ wp option get default_ping_status

$ wp option update default_ping_status closed
Success: Updated 'default_ping_status' option.

As you can see in the below screenshot, the four the settings have been changed. Just about any setting can be tweaked in this way – it is just a matter of find the option name.

The changed comment settings in the WordPress dashboard.

Image: the changed settings.

Automating changes

Hopefully the examples demonstrate how you can use WP-CLI to quickly change settings. If you are making the same changes regularly then you can put together a simple shell script that runs the relevant WP-CLI commands. Here is an example:

$ cat wp-cli-customisations 
#! /usr/bin/env bash
# This script demonstrates how to automate WP-CLI customisations. 
# There are better ways to do this - for instance, the option 
# names and values can be listed in a separate file, and the 
# script could then simply loop through the lines in that file.

# First, let's check if wp lives:
if ! type wp; then
  printf '%s\n' "Failed to execute 'wp' - aborting"

# Here come our customisations:
if ! wp option update comment_registration 1 --quiet; then
  printf '%s\n' "Failed to update comment_registration value"

if ! wp option update default_pingback_flag 0 --quiet; then
  printf '%s\n' "Failed to update default_pingback_flag value"

if ! wp option update default_ping_status closed --quiet; then
  printf '%s\n' "Failed to update default_ping_status value"

The --quiet option used in the wp option update commands suppresses the output (i.e. you don’t get the “Success” message). If you are running the script interactively then you may actually prefer to see the confirmation messages. We just wanted to make you aware that there is the option to suppress the normal output.

Output formats

Now that we have touched on automating tasks we should also mention that the wp option list command can format the output in a large number of formats, including JSON, YAML and CSV. This gives you a lot flexibility for working with the output. For instance, you can use the comma-separated format and then pipe it to grep and awk:

$ wp option list --format=csv | grep -E "^(siteurl|home)" | awk -F',' '{print $2}'

There are various other commands that support different output formats. For instance, the wp option get command supports JSON and YAML and wp option update understands JSON:

$ wp option get active_plugins
array (
  0 => 'two-factor/two-factor.php',

$ wp option get active_plugins --format=json

$ wp option get active_plugins --format=yaml
- two-factor/two-factor.php