Course: Working with the Command Line and WP-CLI
When we think about “using a computer,” we usually think in terms of graphical user interfaces, or GUIs: interfaces (like Microsoft Word, the Mac OS, or the WordPress admin interface) that are specially and attractively designed, that have programs and windows and tabs that visibly open and close, and that let you click things and drag things and drop things and hover things.
For some development tasks, the command line is still both significantly faster and more powerful than any GUI tool.
It wasn’t always this way. The original manner of interacting with a computer is through the terminal, also called the command line: a stripped-down, usually mouse-free interface for entering programming commands directly.
Well, the terminal’s not just a piece of history. For some kinds of tasks, the command line is still both significantly faster to use and more powerful than any GUI tool that exists. This is especially true for a WordPress developer, thanks to one of the coolest pieces of programming ever to hit the WordPress community: the WP-CLI project, which hugely simplifies all kinds of WordPress tasks—everything from “delete the 100 oldest comments” to “update all plugins” to “export the entire database.”
This course is a gentle and thorough introduction to the magical world of the terminal, and to the hypermagical world of WP-CLI. Let’s dive in!
1. What is the Command Line? CLIs from First Principles
Key Point: Defining the Command Line
The command line, or the terminal, is the original way, and still a very useful way, of interacting with a computer.
The command line, command line interface (CLI), or terminal is the original way, and still a very useful way, of interacting with a computer. It does not use a graphical user interface (GUI), and generally does not use a mouse. Terminal commands’ other differences from GUI applications include:
- Terminal commands are simple programs running once.
- Unlike GUI programs which are always waiting for you, most terminal applications run quickly and quit.
- Terminal commands are often dense with meaning and short, because people wanted to type as little as they could get away with.
The fundamental advantage of the terminal/command line is that a competent programmer can often work much more quickly and precisely than if she were clicking and dragging GUI objects. The terminal is also generally the primary tool that gives you the most direct access into the inner workings of a given machine.
Key Point: Windows Users Will Need to Work Harder for a Unix-y Shell
Microsoft Window’s shell is the “odd one out” from the BASH terminal that most other operating systems (all of which are based on or similar to Unix) use. If you use Windows, your best route is to install a Unix-y terminal, but results doing so may vary.
2. Quick Guide: Navigating the Filesystem from the Command Line
Key Point: The Three Essential Commands for Navigating a Unix Command Line
Navigating a filesystem in the Unix terminal makes heavy use of three fundamental commands:
pwd, which finds your “present working directory”
ls, which displays the files in your current directory
cd, which changes your current directory
3. Quick Guide: The Basics of Manipulating Files from the Command Line
Key Point: File and Folder Manipulation from the Terminal
This Quick Guide introduces a number of Unix terminal commands for creating and modifying files and folders:
mkdir foldername: make a new directory.
touch file.ext: create a new file.
cp original.txt copy.txt: copy a file from an old location to a new one.
mv original-name new-namerename a file or folder.
rm file.ext: delete a file.
4. 10-ish Unix CLI File Commands Every WordPress Developer Should Know
Key Point: Breadth of Command-Line Options
This article reviews in detail some of the initially introduced terminal commands, such as
It also introduces the possibility of editing the contents of files using a command-line-compatible text editor such as
nano. Finally, it introduces command-line version control using
5. Mastering the Unix Terminal: Working With Permissions, Networking, and Other Key Concepts
Key Point: Working with Permissions
User file permissions control which of a machine’s users can do particular actions to particular files and folders.
User file permissions control which of a machine’s users can do particular actions to particular files and folders. Three useful commands for navigating permissions from the Unix terminal include:
sudo: temporarily makes the current user the most powerful “root” user, generally after correctly inputting a “sudo password.”
chmod: changes file and folder permissions using a three-number system that varies from 000 (most restrictive) to 777 (most permissive).
chownchanges file and folder ownership between the machine’s users.
Key Point: Network Utilities For the CLI
Command-line network utilities relate to connectivity between machines.
This section quickly reviews command-line network utilities: functions that relate to connectivity between machines.
ssh: securely connect to other servers. (Requires first generating SSH keys, a separate process.)
rsync: remotely transfer files to and from another server.
wget: download content from the internet without SSH authorization.
6. Quick Guide: How and Why to Make a Bash Alias
Key Point: BASH Aliases are Keyboard Shortcuts for the Command Line
A BASH alias creates keyboard shortcuts for commonly typed commands, which can be as short as one letter. This video guide covers the multipart process to writing a BASH alias.
7. The What, How, and Why of WP-CLI: WordPress in Your Terminal
Key Point: What WP-CLI Is
WP-CLI is a program that allows you to use WordPress on the command line.
WP-CLI stands for “WordPress Command Line Interface.” It is a program that allows you to use WordPress on the command line. The core advantages of WP-CLI include:
- Speed: a keyboard-only interface is faster for experienced users than a mouse-and-keyboard graphical interface can be.
- Automation: GUI-less commands are much easier for machines to execute. WP-CLI can be used to, for example, very quickly run a single command on hundreds of WordPress installs.
- Specific powers: WP-CLI has a number of useful tricks that don’t have GUI counterparts.
Key Point: WP-CLI Tricks
The following are some early uses of WP-CLI that show some of its usefulness:
wp core update: Quickly updates the WordPress version on a specific install.
wp core download --force: Re-downloads the core WordPress files, even if the WordPress install is up-to-date.
wp plugin install plugin-slug: Quickly install a plugin.
wp plugin activate plugin-slug: Quickly activate a plugin.
wp plugin update --all: Update all installed plugins.
wp theme update --all: Update all installed themes.
wp db export : Export the WordPress database, optionally specifying a filename.
wp search-replace: Find-and-replace in the WordPress database.
wp media regenerate: Regenerate all image thumbnails.
8. Quick Guide: Installing WP-CLI
Key Point: WP-CLI Installation Steps
This video quick guide walks through getting the WP-CLI software onto your machine. The steps are as follows:
- Open BASH.
- Paste in the command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.pharand hit enter. This will download the file from the URL onto your computer and into your current directory.
- Make sure the above command worked via a run of `php wp-cli.phar –info`. If you get output, keep going. If not, redo step 2.
- Make the
pharexecutable via `chmod +x wp-cli.phar`.
- Rename and move the executable into your PATH, so that you can type
wpto run the command, with
sudo mv wp-cli.phar /usr/local/bin/wp.
- Confirm that everything worked by running
9. Anatomy of a WP-CLI Command: wp database import
Key Point: WP-CLI Commands Can Hook in the Methods of Entire PHP Classes
Registering WP-CLI commands uses PHP syntax that is quite similar to WordPress’s existing hooks system. However, the WP-CLI registration process has one major innovation: it can register in all public methods of a PHP class at once.
The article gives this code example:
WP_CLI::add_command( 'db', 'DB_Command' );
This hooks all the public methods of the (previously registered)
DB_Command PHP class into WP-CLI. So the
DB_Command class’s public
import() method can now be run from WP-CLI with
wp db import, and similar things are possible for all the class’s public methods.
Key Point: WP-CLI Commands Are Specially Documented
The article describes a way of commenting the PHP class’s functions that is especially helpful to command-line users. The documentation for the
import() function begins as follows:
/** * Import a database from a file or from STDIN. * * Runs SQL queries using `DB_HOST`, `DB_NAME`, `DB_USER` and * `DB_PASSWORD` database credentials specified in wp-config.php. This * does not create database by itself and only performs whatever tasks are * defined in the SQL.
This text is actually what will show up in the terminal if the user types
wp db --help.
This course should offer you an accessible introduction to one of a serious developer’s most powerful tools—the speed, simplicity, and directness of the command line—and to the WordPress superpowers of WP-CLI specifically. Thanks for reading, and we’d love to hear thoughts or questions in the comments below!
An outstanding talk on writing custom WP-CLI commands:
A very good article on advanced uses of WP-CLI by its primary maintainer, Daniel Bachhuber:
Image credit: Wkimedia
Add a comment