TIDY REPO The best and most reliable WordPress plugins

We need your help! Please consider taking our poll and help us figure out where to take Tidy Repo next!

WP Cli Header

wp-cli

Andreas Creten, scribu
August 2, 2013

Out of the box, WordPress is simple to use for people of any web experience. But if you work as a WordPress developer, you’ll often find yourself doing the same things again and again. Creating new blogs, upgrading your site, running plugin tests and the like. You’re also probably familiar with the command line because hey, you’re a developer. That’s where wp-cli can help you out.

What’s It Do?

wp-cli gives you access to a fairly long list of command line tools for you to use. What this really means is that you can execute basic tasks on the fly without having to enter the WordPress admin panel, and you can automate tasks by simply stringing them together in a text file.

I won’t go into all the details of the commands, but here are a few examples

  • wp core – The most basic of commands. You can create a new blog, download core from WordPress.org, update your install and generally manage everything. You might end up with a series of commands like this:

    ## Download the most recent WordPress version to your directory
    wp core download
    
    ## Create a wpconfig.php file
    wp core config --dbname=databasename --dbuser=username --dbpassword=databasepw
    
    ## Create a new database using the parameters from the config file
    wp db create
    
    ## And finally, install your WordPress site
    wp core install --title=MySite [--admin_name=user] --admin_email=myemail@gmail.com --admin_password=password
    

    This will install WordPress to whichever directory you are currently in. 5 minute install reduced to even less.

  • wp db – Use this to create, delete or otherwise manipulate your SQL databases
  • wp scaffold – Creates a boilerplate for certain code. This can be very helpful. wp scaffold _s, for instance, will create a new theme pulled from underscores.me
  • wp plugins – Update, add and delete plugins.
  • wp post – Write posts from the command line. Okay maybe not, but you can manage and update posts as needed.

This list only scratches the surface of what wp-cli can do, but it should give you an indication of how much control you get. You can install, manage, update and modify a WordPress all without ever having to log in to the backend.

How’s It Work?

Just note, I am assuming you have some experience in the command line if you are reading this far. If you aren’t, check out this LifeHacker post.

There are a few dependencies for wp-cli, but you should be using them if you are planning to install WordPress. You’ll want a UNIX-like environment (the site recommends OS X, Linux, FreeBSD, etc.). You really just need access to your command line. You’ll also want to have PHP 5.3.2 or later installed. In addition, install Composer for dependency management (though this will be handled by the installer if you do not have it).

From your command line, in your root directory, run


curl https://raw.github.com/wp-cli/wp-cli.github.com/master/installer.sh | bash

This will launch the wp-cli installer which will automatically install all of the dependencies that it needs to run. Assuming everything goes right, you will be ready to use wp-cli. If it does not, I would check the GitHub issues page to see if anyone has had a similar problem.

In order to make the “wp” command run properly, you’ll have to add this line to your .bash_profile file.

# WP-CLI Bash completions
source $HOME/.wp-cli/vendor/wp-cli/wp-cli/utils/wp-completion.bash

To start using wp-cli either create a new directory, or cd into the directory that you want to set-up WordPress. From there, just use wp-cli’s commands to control your WordPress install. There is a long list of things to do but just type any command followed by the word “help” to find out exactly what it does. And check out the post on Wp Tuts+ written by the plugins creator, Scribu, for a basic rundown of what this plugin can do.

A list of possible commands

A list of possible commands


Costs, Caveats, Etc.

wp-cli is well-maintained but it is not incredibly well-documented. If you don’t have experience using the command line and some basic experience with PHP development, then I would not recommend this. You’ll end up swimming in a sea of problems. When I first tried setting this up on my Mac, I had to backtrack several times to get everything right, but I am a man of little experience.

If you are on a Mac, I would consider using Vagrant for setting up your WordPress installs in a local environment. That way every thing stays contained, and you can always start from scratch if you need to. I’ve been using the Varying Vagrant Vagrants package from Jeremy Felt of 10up and it has worked like a charm. wp-cli, as well as many other tools, come pre-installed with the vagrant box. All you have to do is pull the repository off of Github.

Resources

Visit the wp-cli Website


Used this plugin before? Had some problems? Fell in love? Let me know in the comments.