WP-CLI is a very useful set of command line commands which helps one manage the WordPress installation. Most of the “tutorials” found all over the place are simply copied and pasted WP-CLI documentation entries, giving you simply the explanation on how to use it.

We at WP-doin are going to give you the examples on a typical and common WordPress development situations where this tool is going to show it’s true potential.

Prerequisites

We assume that you’re familiar with the command line commands and you understand them:

  1. You move freely between different directories
  2. You’re ok with the basic git commands
  3. You have a local machine setup with git and WP-CLI

Case Studies

Batch User Viewing

Say there’s a custom site created with a number of new user roles and corresponding custom privileges. By default it’s quite hard to manage roles / and the users. Obviously you could go ahead and install a plugin which facilitates user role management.

However it’s not so easy to list all of the user with the corresponding roles, right? Again you could install the plugin that hooks into the users screen and prints the associated role. Continuously following the same rule. In a nutshell it’s not always a good idea to install a number of plugins for these tasks. They will simply overload the installation with enormous amount of code. Which most of the time is not even neccessary. This might impact site load times and negatively affect its performance. Moreover it’s quite frustrating having to install the same plugin for each of our WordPress installations? Why would we even need that if we can simply move between the installations from within the command line?

WP-CLI user list
Why is it handy? Instead of clicking several (hundred) times we’re s

We believe it’s much faster and easier to preview the users instead of logging onto the site, doing several clicks, installing a few plugins to simple get the result.

WordPress site setup

If you’re a busy developer you’re probably installing several sites a month or even a week, don’t you?. The common pattern goes as follows:

  1. Download the latest WordPress (open browser tab / go to wordpress.org / download WP / move WP to your working dir / unpack the archive / open the extracted file in the browser)
  2. Configure the local database
  3. Download and activate the theme framework (mostly repeat the process from above)
  4. Configure the subversion (say git clone repo_url)
  5. Install WordPress locally
  6. Download and activate plugins from the WP dashboard (which is at least 3 clicks per plugin)
  7. Reconfigure wp-config.php file to match the subversion / git repo
  8. Push the site to the repo
  9.  Move the local database to the repo staging database
  10. Start the development

In our opinion this is a horrible PITA! Especially due to the fact of the enormous number of clicks.

What could we do to simplify the whole process?

Assuming that we’re using git, our process will be much simpler:

  1. Configure subversion (say git clone repo_url)
  2. Configure the database
  3. Tell WP-CLI to install new site
  4. Download the theme framework
  5. Activate the framework with WP-CLi
  6. Download the plugins and activate them from command line
  7. Reconfigure wp-config.php file to match the subversion / git repo
  8. Pusth the site to the rep
  9.  Move the local database to the repo staging database
  10. Start the development

Why is the process much simpler? Because everything can be done from the command line (with some exceptions depending on your staging server setup). There’s no reason to wait for the browser to load the WordPress, there’s no reason to click through the dashboard and load all of the plugins. And finally there’s no reason to alt-tab between the command line tool for subversion and the browser. Everything can be simply done from within one place.

Say we’re using XAMPP and our sites lie somewhere within on the D:\xammp\htodcs directory.

Let’s go ahead and fire the command line.

The following couple of lines moved us to our XAMPP development directory, (lines #1 to #5). Cloned the git repository. Downloaded the latest WordPress installation / created the wp-config.php file with some test data and installed it locally. For the purpose of this tutorial we assumed the database has already been created.

Now assuming you have moved the theme and plugin files to the wp-content corresponding directories, we can enable all of them at once with a simple two liner.

And voila our local installation is up and running and we’ve saved a lot of back and forth clicks.

It’s only a tip of the iceberg and a really basic introduction to WP-CLI. In our humble opinion WP-CLI power lies in the reduced amount of alt-tabbing and back and forth clicking between the command line tool, WordPress install and the database management tool.

We at WP-doin are using custom bash scripts and VVV setup to facilitate site creation. If you’re into the scripting you can preview a sample bash script combined with the WP-CLI, which both creates a database and downloads latest WordPress install.