Administering WordPress from the Command Line through WP-CLI

Working with the command line can be intimidating if you’re used to a nice graphical user interface (GUI), but it’s worth getting familiar with. There’s a lot of mundane tasks in web development can be automated with good set of scripts and shortcuts.

It’s also often much faster and simpler to accomplish a task with the command line, and in this respect WordPress is no exception. WP-CLI makes it possible for you to get your WordPress related tasks done with the help of a set of commands.

You might have a natural question in your mind: if the WordPress admin interface is so gorgeous and user-friendly, why would someone like to use the command line interface? There are two key reasons:

People find keyboard faster than the mouse: Instead of using mouse, a power user always prefer to use keyboard shortcut to accomplish a task. In general, using keyboard shortcuts is 3 times faster than clicking a mouse.

Automation:To make several commands execute automatically, you just need to put them into a single text file. Automation saves a lot of your precious time.

So in this guide, I’m going to give you a taste of WP-CLI: a command line interface for administering WordPress sites.

About WP-CLI

WP-CLI, which stands for WordPress Command Line Interface, is an open-source set of command-line tools that enables you manage all aspects of your WordPress site, from installation to publication and everything in between, directly through the command line. With WP-CLI, most of the admin activities that are accomplished via WordPress dashboard in a web browser can be performed by typing a command at the command prompt. Initially created by Andreas Creten and Cristi Burcă, WP CLI is now being maintained by Daniel Bachhuber.

System Requirements:

Before you’re going to get your hands on WP-CLI, it’s quite crucial for you to be familiar with its basic system requirements. Below are necessary requirements to get started with WordPress Command Line Interface:

One more thing to remember, working with WP-CLI on a web server requires you to be familiar with and access to SSH. You can easily enable SSH access via hosting control panel (cPanel). Additionally if your hosting environment is Grid, you’ll have WP-CLI pre-installed and configured.

Getting Started with WP-CLI

Installing WP-CLI:

As WP-CLI doesn’t ship with the WordPress installation, you need to download and install its recent version by executing a few commands via SSH. To do this, follow the steps given below:

Log into your hosting account using SSH.

Once you’ve logged in successfully, run the curl or wget command to download the latest phar file of WP-CLI. For example:

After putting above two lines in the.bash_profile file, save and close it. Then reload the file with source ~/.bash_profile to apply the changes.

Now you may try running php wp-cli.phar –info to make sure changes have been made to the .bash_profile.

Making WP-CLI Executable:

In order to remove “php” preface from the php wp-cli.phar –info command, you’re required to make the wp-cli.phar file executable. This can be done by editing its permissions as follows:

chmod +x wp-cli.phar

Now try running ./wp-cli.phar –info.

Renaming and Moving WP-CLI:

As per you convenience, you can rename wp-cli.phar file to something like ‘wp’ and move it to somewhere in your PATH to make it available from anywhere. For example:

sudo mv wp-cli.phar /usr/local/bin/wp

Now, you’ll be able to type just wp instead of wp-cli.phar. To check, try running wp --info.

Tab Completions:

If you are using Bash and want tab completions, WP-CLI provides you with a tab completion script. You just need to download wp-completion.bash file and then load it from ~/.bash_profile.

source /FULL/PATH/TO/wp-completion.bash

Make sure to run source ~/.bash_profile afterwards.

Using WP-CLI to Manage WordPress

Once you’ve installed WP-CLI, you would be able to use it for managing WordPress. WP-CLI has a number of commands that are divided into two types: internal commands maintained by the WP-CLI team and community commands written by other thirty-party people. Below I’ve mentioned a few internal commands, performing the functionality offered by WordPress.

Installing WordPress:

After installing WP-CLI, when you type ‘wp’(the command to communicate with WP-CLI interface), you’ll get a message, asking you to run ‘wp core download’ command.

To install WordPress through command lines, you first need to create a folder where you wish to install WordPress. You can do this via mkdir (make directory) command. For example, below I’ve created a folder named wordpress under projects folder.

To edit content of a particular post, the command given below can be used:

wp post edit <id>

Where <id> is the “ID of the post” that you wish to edit.

Performing Database Queries:

With WP-CLI, you can also perform database operations like backup, restore, optimize, repair etc. For instance, you can back up your WordPress database to a file using the following command:

wp db export

Conversely, you can import database from the exported file:

wp db import filename.sql

To optimize your WordPress database, you can use the following:

wp db optimize

As well, using wp db cli command, you can open an interactive MySQL session using your WordPress credentials.

Updating WordPress:

To update WordPress and its database, type following commands:

wp core update
wp core update-db

You can even update all your WordPress plug-ins and themes by typing:

wp theme update --all
wp plugin update --all

To know which WordPress version your site is running on, type the command mentioned below:

wp core version

Conclusions

As you can see, WP-CLI is a powerful tool, especially when you start to tie it with other scripts and begin to automate common functions. Do you use WP-CLI in your workflow? If so, is there anything you would add?