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:

  • WordPress Version:5.2 or later
  • Scripting Language: PHP 5.3.2 or later
  • Operating System: UNIX-like environment such as Linux, OS X, FreeBSD or Cygwin.

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:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Or:

wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

To check if the phar is downloaded correctly, run a simple command:

php wp-cli.phar --info

If you get output like this:

PHP binary: /usr/local/bin/php
PHP version: 5.6.5
php.ini used: /usr/local/lib/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI global config: blank
WP-CLI project config: blank
WP-CLI version: 0.18.0

Then you would be able to use WP-CLI. In case you don’t get anything, you need to try again.

Moreover, you may go through alternative install methods. Upgrading WP-CLI requires the same procedure to be followed.

Using a Custom PHP Binary:

If you’re running the MAMP server, you may get a MySQL error due to the wrong PHP used in your PATH. To fix it, you have to edit the .bash_profile (nano ~/.bash_profile) file as follows:

export MAMP_PHP=/Applications/MAMP/bin/php/php5.6.5/bin
export PATH="$MAMP_PHP:$PATH"

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.

mkdir ~/projects/dev/wordpress

Now navigate to created wordpress folder.

cd ~/ projects/dev/wordpress

Then, run wp core download command to download the latest version of WordPress.

wp core download --version=4.1

Once the WordPress package gets download, create wp-config.php file using the wp core config command:

wp core config --dbname={YOUR DATABASE NAME} --dbuser={YOUR DATABASE USERNAME} --dbpass={YOUR DATABASE PASSWORD}

Next, we’ll install WordPress using wp core install command which require us to define following parameters:

  • url: Your website’s domain name.
  • title: Title of your site.
  • admin_user: The admin username that you would use to log-in your WordPress site.
  • admin_password: The password associated with the admin username.
  • admin_email: An email address to get noticed for any site-related issue.

So let’s finish WordPress installation by running wp core install command as follows:

wp core install --url={YOUR DOMAIN NAME} --title={THE TITLE OF YOUR SITE} --admin_user={YOUR USER NAME} --admin_password={YOUR PASSWORD} --admin_email={YOUR EMAIL}

If everything goes fine, you’ll see a message saying:

Success: WordPress installed successfully.

Means, you have successfully installed WordPress. To check, visit your site in the browser.

Managing Themes:

WP-CLI offers numerous commands to work with themes. For example, if you want to install Twenty Fifteen theme, then you need to use the command line given below:

wp theme install twentyfifteen

To check whether a theme is installed correctly or not, you can use wp theme list command to get a list of installed themes.

To activate newly installed Twenty Fifteen theme, you just need to type:

wp theme activate twentyfifteen

While running wp theme status command, you can check the status of your all themes.

Managing Plug-ins:

WP-CLI lets you manage your WordPress plug-ins in the same way you manage your themes. For instance, suppose you want to install W3 total cache plug-in, then you can install it by typing:

wp plugin install w3-total-cache

And activate it using the following command:

wp plugin activate w3-total-cache

And, of course, you can update it as well:

wp plugin update w3-total-cache

Managing Content:

You can even compose, edit and manage posts through the wp-cli interface. For example, to create a post, you can use the following command:

wp post create ./post-content.txt --post_title='My First WordPress Post' --post_date='2015-02-12 05:00:00'

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?

Original terminal icon by Dax Hansen.

About Ajeet Yadav

Ajeet is an experienced WordPress developer who is working with WordpressIntegration - Developing WordPress themes from PSD. In his spare time, he writes about different topics related to HTML5, Responsive, WordPress, eCommerce, and JavaScript to share his work experience with others. For weekly updates, follow @Wordpress_INT.

1 Response

  1. Marcell

    Nice tool.. thanks for the tip
    I guess there is a typo about the WP version.
    Unfortunately my web hosting doesn’t support PHAR… any way to overcome this?

Leave a Reply