WP-CLI 1.0 Released! Manage Your WordPress Site from Command Line

The first stable 1.0 version of WP-CLI was released on November 29th 2016, a milestone in the history of WP-CLI, which is a command line interface for WordPress created 5 years ago. Complete changes over previous versions can be found on wp-cli.org blog. In this tutorial, we will look at using WP-CLI to do basic WordPress website management.

wp cli

How WP-CLI Can Benefit You

  • Manage WordPress tasks more efficiently
  • Save you a lot of clicks, page loads and time.
  • Do things that you can not do in the graphical WordPress dashboard

How to Install WP-CLI on Linux Server

SSH log into your Linux server and download the wp-cli.phar archive using curl or wget.

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


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

Then verify if it works using the following command:

php wp-cli.phar --info

You will see something like below if it’s working:

PHP binary:	/usr/bin/php
PHP version:	7.0.13
php.ini used:	/etc/php/php.ini
WP-CLI root dir:	phar://wp-cli.phar
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	1.0.0

Next, we add executable permission to the archive file, move it to /usr/local/bin/ and rename it to wp.

chmod +x wp-cli.phar

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

So now we can just type wp instead of php wp-cli.phar.

wp --info

How to Use WP-CLI

When running wp-cli commands that changes files and directories of WordPress site, you need to run the command as the web server user, which is typically named www-data, apache, or nginx.

When no option is given to sudo, it runs the command as root user. To run the command as another user, use -u option like below.

sudo -u www-data wp plugin update --all

The above command will run wp plugin update --all command as www-data user. wp-cli commands that don’t change files or directories but just get information can run as normal user.

Another thing to note is that you need to change your working directory to the WordPress installation directory before running any wp-cli commands.

cd /var/www/wp-site/

With that out of the way, let’s see how to manage WordPress core, WordPress themes and WordPress Plugins using WP-CLI.

Managing WordPress Core

Check WordPress core version.

wp core version

Display extra information of WordPress core.

wp core version --extra

Update WordPress core

wp core update

Update WordPress database

wp core update-db

Check MD5 hash

wp core verify-checksums

Managing WordPress Theme

List installed themes

wp theme list

Search for a theme

wp theme search <theme_name>

Install a theme

wp theme install <theme_name>

Install a theme from specified URL.

wp theme install http://example.com/theme_name.zip

Active a theme

wp theme active <theme_name>

Update a theme

wp theme update <theme_name>

Update all themes

wp theme update --all

Uninstall a theme

wp theme uninstall <theme_name>

Managing WordPress Plugins

List all plugins installed on WordPress. The name field shows you the slugs used by your plugins.

wp plugin list

List plugins that have update available.

wp plugin list --update=available

Display output in json or csv format.

wp plugin list --format=json

wp plugin list --format=csv

Install plugin from WordPress plugins directory.

wp plugin install <plugin_name>

Install plugin from a URL.

wp plugin install http://www.example.com/plugin_name.zip

Activate a plugin

wp plugin activate <plugin_name>

Deactivate a plugin

wp plugin deactive <plugin_name>

Uninstall a plugin

wp plugin uninstall <plugin_name>

Update a plugin

wp plugin update <plugin_name>

Update all plugins

wp plugin update --all

Backing Up WordPress Database and Files

Run the following command to back up database. Note that it is very insecure to place the SQL file under the root of your website. Instead, place it at somewhere else such as your home directory.

wp db export ~/backup_db.sql

To backup the Files, we can use the good old tar archive utility like below.

sudo tar -cpzvf ~/wp-file-backup.tar.gz /var/www/wp-site/

A Simple Shell Script that Auto Update WordPress, themes and plugins

We can write a shell script that update WordPress core, themes and plugins automatically. Create a blank .sh file in root user’s home directory.

sudo nano /root/auto-update-wp.sh

Put the following lines into the file.

#! /bin/bash
cd /var/www/wp-site/

sudo -u www-data wp core update

sudo -u www-data wp core update-db

sudo -u www-data wp theme update --all

sudo -u www-data wp plugin update --all

Save the script and add executable permission.

sudo chmod +x auto-update-wp.sh

Then add a new entry in root user’s crontab.

sudo crontab -e

The following entry will check and perform auto update at 4 am each day.

0 4 * * * /root/auto-udpate-wp.sh

That’s it!

Of course, this is just an introduction. WP-CLI is very extensible and can do many more things. As always, if you found this post useful, then subscribe to our free newsletter. You can also follow us on Google+ Twitter or like our Facebook page.

Rate this tutorial
[Total: 2 Average: 4.5]