In the last two articles we looked at installing and configuring WP-CLI and using WP-CLI to install WordPress. This article looks at managing WordPress settings.

Managing options

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

You can list all options with wp option list. The list will be rather long, so you probably want to check specific options instead. For that, wp option get is your friend. For instance, you can use the command to check the admin email address:

$ wp option get admin_email
support@catalyst2.com

And you can change values via the wp option update command:

$ wp option update admin_email premium@support.catalyst2.com
Success: Updated 'admin_email' option.

$ wp option get admin_email
premium@support.catalyst2.com

Customising WordPress

If you regularly install WordPress you probably make the same configuration changes time and again. With WP-CLI, you can quickly make the changes you want.

A good example are the default comment setting in WordPress. By default, non-registered users can leave comments on a WordPress site. To some people that is a feature, while others see it as a bug. If you are in the latter camp then you need to do quite a bit of clicking around to tweak the setting: you need to navigate to Settings > Discussion; tick Users must be registered and logged in to comment and save the setting. With WP-CLI you can instead run a single command. You can even add the command to a script. Before we get to that, let’s first look at some more examples.

Some of the default comment settings in WordPress.

Image: the default comment settings in WordPress.

Changing settings is easy enough. However, it can be tricky to find the name of a setting. Luckily, the Option reference page in the WordPress Codex lists quite a few options. For instance, the page shows that the name of the option “Users must be registered and logged in to comment” is comment_registration. It also shows that possible values are 0 (false) and 1 (true).

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

$ wp option get comment_registration
0

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

Similarly, you can disable comments on all new posts by setting default_comment_status to “closed”:

$ wp option get default_comment_status
open

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

And while we’re at it, let’s disable pingbacks as well:

$ wp option get default_pingback_flag
1

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

$ wp option get default_ping_status
open

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

As said, Just about any setting can be tweaked. It is just a matter of find the option name.

The changed comment settings in the WordPress dashboard.

Image: the changed settings.

Automating changes

If you regularly make certain changes you can use a script to automate the job. For instance, this Bash script makes the above tweaks:

$ 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"
  exit
fi

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

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

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

The --quiet option used in the wp option update commands suppresses the output (i.e. you don’t get the “Success” messages). If you run the script interactively then you may actually prefer to see the confirmation messages. In that case you can simply leave out the --quiet option.

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 instance, you can use the comma-separated format and then pipe the output to grep and awk:

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