Using WP-CLI On The Pantheon Platform

WP-CLI is a command-line interface to WordPress. It provides a wide range of utilities for managing your WordPress site. Virtually any action you can perform through the WordPress admin, you can also do with WP-CLI.

To use WP-CLI on the Pantheon Platform, you'll first need to install Terminus on your local machine. Terminus is a command-line interface for managing your Pantheon sites, and is used to proxy commands from your local machine to your Pantheon environment.

Once you've installed Terminus locally, and verified it's working correctly, you're ready to use WP-CLI. However, if you haven't already, you may want to consider installing WP-CLI locally for use in your local environment.

Getting Started With WP-CLI

When working on the command-line, it's worth remembering the adage: "with great power comes great responsibility". WP-CLI will let you perform incredible operations on your WordPress site (including dropping your database), for better or for worse. Only you know the true intention of the command you're invoking.

Before you use WP-CLI for the first time, here are a few global parameters you should be aware of:

--path=<path> - Specify the path to WordPress. If this parameter isn't provided, WP-CLI will look upward from its current directory to attempt to find WordPress.

--url=<url> - Identify the request as from given URL. In WordPress Site Networks, this argument is how the target site is specified.

--user=<id|login|email> - Run the command as a particular user. By default, WP-CLI commands are executed anonymously.

Each of these global parameters define the context under which the command is run. They are important pre-condition statements to WordPress of how it should understand the request.

Now that we've covered the most important basics, let's run a command:

option get is the command itself (docs). home is the key for the option we're requesting.

<site> and <env> tell Terminus which site and environment to run the command in, respectively. These arguments can be provided automatically if you execute Terminus commands from a directory containing a .env file.

The first part of the output is Terminus telling you which command it's running, and where. The last line, "https://pantheon-demo.pantheonsite.io", is the response of wp option get.

Feeling comfortable with WP-CLI? Here are a few of many commands you may find helpful in your journeys:

wp search-replace - Search for and replace specific strings in the database. Use --dry-run to perform a test run of the operation, and see how it will affect your database (docs).

wp media regenerate - Regenerate image thumbnails for one or more attachments (docs).