Whether you’re a developer or an admin overseeing multiple WordPress sites, I’m sure you’ve thought to yourself: “I wish I could do this faster.” From creating a fresh install for testing to updating the same plugin on multiple sites, there are so many tasks you’ll find yourself doing over and over again.
WP-CLI is the answer to your woes and one of my favorite time-saving tools for web development.
This is the final post in our six-part series focusing on WordPress for advanced developers. This series follows on from our popular WordPress Development for Intermediate Users, which introduced you to some meaty coding topics, including theme development in detail, making themes customizer-ready, building plugins, custom post types and taxonomies, queries and loops, custom fields and metadata, and localization.
WP-CLI is a command line utility (hence the CLI part) that gives you control over may aspects of WordPress. In this tutorial, I’ll show you how to use WP-CLI, how to create powerful bash scripts for even more powerful automation, and how to manage multiple WordPress sites at once.
That’s a tall order; we’d better get started!
Note: It’s important that you have a working knowledge of PHP as this is the foundational language of WordPress for this series, which covers advanced topics aimed at developers. I’ll be referring to code snippets throughout this series.
Installing WP-CLI
Installing WP-CLI is super-easy. Just issue the following commands, which can also be found on the WP-CLI home page:
.gist table { margin-bottom: 0; }
Regretfully, Windows has limited support; you get the most mileage out of WP-CLI on Unix-like environments. You can find some additional notes on Windows installation in the documentation. If you’re having trouble, use what we learned in the terminal tutorial and SSH into a remote server, which is most likely to be a Unix environment, and practice there.
Basic WP-CLI Usage
The use of WP-CLI is easy and intuitive once you’ve used a few commands. It always starts with the base wp
command, followed by one or more subcommands. Subcommands are used for grouping, which makes things logical. For example, here are commands to get and update a specific option in the database:
.gist table { margin-bottom: 0; }
One of my favorite – and most frequently used – commands is search-replace
. This enables you to search-replace a term within the WordPress database. It handles serialized strings properly; unserializing them, making the replacement and the re-serializing them.
.gist table { margin-bottom: 0; }
As you can see, using WP-CLI is not difficult. Take a look at the commands documentation on the WP-CLI website for more information. I’ll be going through some more commands shortly, but first I want to show you some advanced setup and options.
Global Parameters
WP-CLI has a bunch of global parameters that can be added to any command. You can read about them on any documentation page in the right-hand sidebar block. I’ll go through the most useful ones.
–quiet
This parameter will suppress info messages that are normally displayed when executing a command. I use this when creating bash scripts that execute a bunch of WP-CLI commands at once. More on this later.
–ssh
By adding this parameter, you can execute the WP-CLI command on a remote server instead of locally. This is what we’ll be using to control multiple WordPress instances by issuing local commands.
–path
You can explicitly set the path of the WordPress installation. Normally, WP-CLI is run from within the root directory of the WP installation. If this isn’t the case, you can use --path
to tell WP-CLI where WP is.
–prompt
This will prompt you to fill out all parameters. Useful if you don’t know the parameters off the top of your head or if you add WP-CLI as part of a larger script that others will use.
–allow-root
Not a global parameter per se because it doesn’t apply to all commands, but I use it sometimes on remote servers where I must use sudo for some reason. If you use sudo, you must use this parameter to make sure WP-CLI performs this action. That said: try to avoid using sudo for commands when possible.