An Introduction to WP CLI

WordPress is a nice platform, but setting up a fresh install or doing maintenance work can be a bit of a hassle. Especially when you need a fresh install every odd week or-so (like me), or you have a lot of WordPress sites to maintain (again; like me). That’s why I love WP CLI.

WP CLI is a command line interface for WordPress. It allows you to install, setup, control and maintain WordPress through your command line. It was initially created by Andreas Creten and is now being maintained by Cristi Burcรข (better known as Scribu). And is currently being developed very actively. Doing things through the command line might seem like a bit of a chore at first, but if you’re actively involved in WordPress plugin or theme development, you’re probably already used to using a command line tool for version control like GIT or SVN. Getting WordPress in on that action is just the next logical step. If you’ve never used the command line before, you might want to check out some other sources before continuing with this tutorial. This tutorial assumes you are at least familiar with SSH. If you’ve got that down, we’ll start with WP CLI! It’ll make WordPress development easier and more fun, I promise… ๐Ÿ˜‰

Installing WP CLI

I use WP CLI on my local machine and on my staging server. We’re going to be installing WP CLI right through the command-line. On your local machine you’ll of course have instant command line access and on a staging server you can use SSH to get command line access. Once you’ve logged in successfully, most servers will be able to install WP CLI for you with just one line:

[/crayon]
If you’re using MAMP there’s a bit more to it… check out these alternate install methods.

If everything went well, you are now able to use WP CLI! Congratulations! ๐Ÿ™‚

Your First Commands

Okay, I don’t want to give away all the awesome immediately but I just can’t help myself;
the biggest perk to working with WP CLI is update automation. Updating WordPress core is as easy as running

[/crayon]
Now, you might say ‘well, that’s nice… but having to remember and type that stuff is a lot harder than just clicking that big blue button’. Well, you’ve got a point, but what if you’d have to click that blue button a bunch of times? What if you needed to update plugins?

[/crayon]
bash

Yep. that’s right. With one simple command, the plugins in your WordPress install can be updated. Of course we have to be smart around updates; there’s always something that can go wrong. That’s why it’s always a good idea to backup the database before updating… fortunately this becomes super-easy with WP CLI:

[/crayon]
Now, when we combine these commands in one simple bash-workflow, we get to safely update every part of a WordPress installation. And if we tie that bash-script to a simple alias, we get to run this stuff with just one simple command.

[/crayon]

Installing your Favorite Plugins

I know WordPress.org has an option to mark your favorite plugins, but i’d much rather automate that stuff entirely when I start a new project. So I made a little bash file of the plugins I use on every project;

[/crayon]
Running this script on a fresh install, will download, install and activate four plugins for me. And if I get greedy I can add as many plugins to this bash-file as I want. I told you this was going to make your life easier ๐Ÿ˜‰

Creating New Installs

WP CLI can even create new and fresh installs for you. It just needs some valid database information. This can be very handy on your local machine or a staging server where you’ll need fresh installs often. The only thing I didn’t like about this solution was me having to create a database every time I needed a fresh install. So I usually work with one big dev-database where multiple WordPress versions coexist. I can do this by changing the prefix of each WordPress install. Next to this being very easy for development, changing the prefix is great for security reasons.

[/crayon]
Now imagine this script, and combine it with the auto-installing favorites script and with one command, you’ll be good to go. If you have any more great commands, ideas or workflows, please leave a comment! I’d love to hear what you’ve come up with!

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

About Luc Princen

Luc Princen is a developer and designer from The Netherlands. He's co-founder and technical lead at Chef du Web where he specializes in pushing WordPress to the limit.

Related Articles

6 Responses

  1. Paul Ratcliffe
    Paul Ratcliffe November 19, 2013 at 14:51 | | Reply

    WP CLI looks like it might be interesting for a number of reasons (and can do a lot more than is listed here – see http://wp-cli.org/commands/), but it’s not something I’d use for managing updates to core (or plugins) right now. Having the ability to update easily from the command line is great, but if something goes wrong where is the rollback functionality? This is where GIT, SVN, Mercurial etc. come in to their own, especially if you’re managing a large number of WordPress sites.

  2. Marko Heijnen
    Marko Heijnen December 17, 2013 at 04:47 | | Reply

    You can downgrade if you want. I run always trunk on my sites because I trust that. The same goes for the plugins/themes I use. So if you don’t trust something when updating then maybe you shouldn’t use that in the first place.

  3. Luc Princen (@LucP)
    Luc Princen (@LucP) December 17, 2013 at 05:38 | | Reply

    If stuff regularly goes wrong when you update you are using badly written plugins and themes; WordPress is very VERY conscious about backwards compatibility.

  4. skara
    skara March 27, 2014 at 15:58 | | Reply

    Thanks for this introducing.
    I fixed some typos (? – admin_name has to be admin_user) and adjusted it a bit to my own needs. If anyone is interested -> https://gist.github.com/skara/9569323a2db0bf57d38f

  5. Jonathan L. Keller
    Jonathan L. Keller May 28, 2014 at 04:53 | | Reply

    Ahh I am so glad I stumbled across this. I spend way too much time installing WordPress. Your guide is going in my bookmarks my friend. Thanks a bunch.

  6. Fahad Rafiq
    Fahad Rafiq October 22, 2014 at 14:25 | | Reply

    Managing multiple WordPress websites is a hectic job, as you have to logging to each site and setup/install plugins and settings for each site independently.

    Luckily for those well versed to work in command line, they can use WP-CLI to manage multiple WordPress sites with just a few commands. WP-CLI comes with built-in commands specific just to manage WordPress websites.

    Through WP-CLI you can install, manage plugins and themes. Control settings such as cron-job, comments etc.

    Learn a few basic commands here: http://www.cloudways.com/blog/how-to-use-wp-cli/

Leave a Reply