Do you own a (virtual or actual) server? Do you like command-line interfaces? Are you bored with the regular WordPress admin panel and longing for an exciting WordPress administration experience? Then it's time to try out the one and only WordPress Command-Line Interface, a.k.a. WP-CLI!
All those cheesy commercial lines aside, we'll be covering a very advanced tool today. In this part of the "Toolbox of the Smart WordPress Developer" series, we're going to go through WP-CLI, a one-of-a-kind WordPress tool that gives us the opportunity to manage our WordPress installations via the command line.
Who Needs GUIs Anyway, Right?
To be honest, I was never a fan of command-line interfaces. I don't own a Mac, I'm not comfortable with Linux distributions, and I don't like using SSH in my server at all. I'm more of a graphical user interface (GUI) guy.
Why do I like GUIs? Well, mostly because they're easier to use. And with software that knows what "user experience (UX)" means (like iOS, Facebook and WordPress), it becomes an enjoyable experience. But it's merely my choice to use GUIs. Some people like to take the high road and stick with command-line interfaces (CLIs).
Yep. Some people like to tap on their keyboards to give commands. Some people like to bypass GUIs while working. Some people like to feel in charge. Some people love command-line interfaces.
Despite my primary choice being graphical user interfaces in software, getting to know WP-CLI was a very entertaining experience—so entertaining that I wanted to share my experience with you and let you live it as well.
Getting Started With WP-CLI
You might wonder if WP-CLI will be worth your time and effort—after all, if you're like me and command-line interfaces are kind of an uncharted territory for you, you may think that it's going to be hard and time-consuming. So, before starting off with WP-CLI, I want you to know that installing and using WP-CLI isn't hard at all.
Having said that, WP-CLI has a few system requirements:
- A UNIX–based interface like OS X, Linux distributions or FreeBSD
- PHP version 5.3.2 (or later)
- WordPress version 3.5.2 (or later)
If your system meets these requirements, you're good to go!
Installing and Using WP-CLI
If you know a thing or two about command-line tools, installing WP-CLI will be a breeze. And if you don't, it's still going to be surprisingly easy. All you have to do is get
wp-cli.phar from the GitHub repository of WP-CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
That's it. Seriously. If you don't believe me, type in
php wp-cli.phar --info to see if it works.
You can continue using WP-CLI by starting every command
php wp-cli.phar, but you don't have to do that. By typing the following two lines of commands, you can use the
wp alias instead of
chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
Piece of cake! Now try to run
wp --info to see if everything works correctly.
In order to control your WordPress installation with WP-CLI, you need to go into your WordPress root folder:
After that, you can use the built-in commands to officially start using WP-CLI. Here's an example:
wp plugin install wp-super-cache
The Built-In Commands of WP-CLI
There are more than 30 built-in commands that come with WP-CLI. Let's get to know each one:
cache: Lets you work with the object cache.
cap: Lets you manage user capabilities.
cli: Lets you display information about WP-CLI.
comment: Lets you manage comments.
core: Lets you download, install, update and manage a WordPress installation.
cron: Lets you manage WP-Cron events.
db: Lets you work with the database.
eval: Lets you execute PHP code.
eval-file: Lets you execute a PHP file.
export: Lets you export your WordPress content into a WXR file.
help: Helps you with WP-CLI commands.
import: Lets you import a WXR file into your WordPress installation.
media: Lets you work with your site's Media Library.
menu: Lets you manage WordPress menus.
network: Lets you manage network custom fields.
option: Lets you change options.
plugin: Lets you install, delete, update plugins and do tons more things.
post: Lets you work with posts (and by "posts", I mean all types of posts).
rewrite: Lets you manage rewrite rules.
role: Lets you manage user roles.
scaffold: Lets you generate starter codes for things like themes, plugins, custom post types and such.
search-replace: Lets you perform search and replace operations.
shell: Helps you evaluate PHP statements and expressions interactively, from within a WordPress environment.
sidebar: Lets you manage sidebars.
site: Lets you do site-wide operations.
super-admin: Lets you list, add and remove super admins (for multisite installations).
term: Lets you manage terms.
theme: Lets you manage themes.
transient: Lets you manage transients.
user: Lets you manage users.
widget: Lets you manage widgets.
Achieving Even More With WP-CLI
Custom WP-CLI Commands: "Making a command is easy", WP-CLI says. It's probably true, because there's a ton of useful community commands and community packages. And with this ability, theoretically, WP-CLI can offer infinite functionality.
WP-CLI Configuration: Did you know that you can configure WP-CLI? You can. You can do things like setting the path to WordPress files, setting the WordPress user, disabling some sub-commands, or loading a PHP file before running each command.
WP-CLI Tools: There are tools that use WP-CLI, including a Node.js wrapper, some helper aliases, Vagrant boxes, and even a graphical user interface for this command-line interface. (Why? Because while bypassing the default GUI of WordPress to work with WP-CLI, you might need a GUI. Makes sense.)
Wrapping Up for Today
As I mentioned before in this article, giving WP-CLI a try was a lovely, fun experience, even though I prefer graphical user interfaces instead of command-line interfaces. So, even if you're not sure about installing WP-CLI on your server, I highly recommend that you give WP-CLI a shot. Even if you decide that you're not going to use it, it's still going to be an entertaining half an hour.
See you in the next part, where we'll go over some other tools that we've gone through here in Tuts+ Code (so that we don't have to re-review in this series).