Using wp-cli for Fun and Profit

See how you can perform common WordPress tasks faster or even automate them by using the power of bash.


What Is wp-cli?

wp-cli is a tool for controlling WordPress through a console window.

When the WordPress admin interface is so beautiful and easy to use, the natural question is: why would you ever use a command-line interface?

There are two main reasons:

  • The keyboard is faster than the mouse - For power users, typing a command can be orders of magnitude faster than pressing a button in a web browser.
  • Scripting - You can put several commands in a text file and have it executed automatically.

In this tutorial I'm going to give you a taste of what wp-cli is good for.


Installing wp-cli

Before using it, you'll need a few things:

  1. UNIX-like shell - If you're on a Mac or on Linux, you can run wp-cli on your local machine. If you're on Windows, you can log into your Linux server via Putty and install wp-cli there.

  2. PHP 5.3+ and the php-cli utility - You can see if you have these by running the following command:

  3. Git - This is what we'll use to install and update wp-cli.

The wp-cli project is still in it's infancy, so the best way to stay on top of recent versions is by cloning it from github:

~/git/wp-cli is the directory where wp-cli will be installed. Feel free to change it to whatever you want.

That's it. Now the wp command should be available:

`wp` output

On Multisite

If you want to use wp-cli on a multisite install, you'll have to decide which blog you want to operate on:

To avoid having to pass the --blog parameter for each command, you can store it in a specially-named file:

wp-cli will read that file if there's no --blog parameter.


Handling Core

First, let's see what version of WordPress we're dealing with:

`wp core version` output

To perform an update, you just need to write:

`wp core update` output

Handling Plugins

Let's see what plugins we have installed:

`wp plugin status` output

Huh, it seems there's an update available for Akismet. Let's install it:

`wp plugin update` output

Now let's install and activate a plugin from wordpress.org:

`wp plugin install` output

Also, you can install the development version of a plugin:

Oh, and look, there's a new command available now:

`wp sitemap help` output

We can quickly switch a plugin from active to inactive and vice-versa:

`wp plugin toggle` output

This is a good way to debug activation hooks.

Similarly, you can run a plugin's uninstall procedure without deleting the plugin files:

And, of course, you can delete the plugin as well:


Handling Themes

We have a few commands for working with themes too:

`wp theme status` output

Unlike plugins, you can only have a single theme running at a time, so activating a theme will automatically "deactivate" the previous one:

And here's a little trick to go into the directory of a particular theme:


Generating Data

If you're writing a theme and you want to style the pagination, you're going to need a lot of posts. Here's the quickest way to get them:

`wp generate posts` output

If you want to style a list of users, you can generate some of them too:

`wp generate users` output

You can also create individual users:

`wp user create` output

Creating Export Files

You might want to periodically export your content to a WXR file.

`wp export` output

You can pass additional parameters to limit what content is exported, such as --category, --start_date etc.


Changing Options on the Fly

There are straightforward commands for CRUD operations on options:

You don't want to do this on a regular basis, as most options are constrained to certain values. But it can come in handy in scripts.


Database Operations

If you want to make a backup of the database, just write:

`wp db dump` output

Or perhaps you need to do a quick query to find when the last post was published:

Opening an interactive MySQL session to do some diagnostics is just as easy:


Running Arbitrary Code

Sometimes, the only way to tell wp-cli what you want is by describing it in PHP code:

With the above command, wp-cli will first load WordPress and then load and execute your PHP file.

This is useful in deploy scripts or for other complex actions that can't be achieved using built-in commands.

You can also pass PHP code inline:


Creating Your Own Commands

Believe it or not, wp-cli is written mostly in PHP. Each command is a class, with each method representing a subcommand.

The neat thing is that you can make your own class, put it in a plugin and wp-cli will automatically recognize it as one of it's own. A detailed tutorial for creating commands is available in the project wiki.


Conclusion

I hope I've convinced you to at least give wp-cli a try. If you've found a bug or if you have a feature request, consider opening an issue.

Have an interesting use case for wp-cli? Please share it in the comments below.

Tags:

Comments

Related Articles