Use WP-CLI To Manage Your WordPress Site From Command Line

April 6, 2020

WP-CLI is a command line interface for WordPress.  In this tutorial, we will look at using WP-CLI to do basic WordPress website management.

How WP-CLI Can Benefit You

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

wp cli

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



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.3.8
php.ini used:         /etc/php/php.ini
WP-CLI root dir:      phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:    phar://wp-cli.phar/vendor
WP_CLI phar path:     /home/linuxbabe
WP-CLI packages dir: 
WP-CLI global config: 
WP-CLI project config: 
WP-CLI version:       2.3.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

When a new version of WP CLI comes out, you can run the following command to update.

sudo wp cli update

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 to the latest version.

wp core update

Update WordPress core to the latest minor version, instead of the main version.

wp core update --minor

Check MD5 hash

wp core verify-checksums

Managing WordPress Database

Update WordPress database

wp core update-db

Optimize database

wp db optimize

Repair database

wp db repair

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

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

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

If your server has multiple WordPress installations, then you may want to run the following command to allow the www-data user to write the /srv/http/ directory because WP-CLI will cache downloaded files to that directory, so WP-CLI doesn’t have to download the same files again when updating the next WordPress installation.

sudo setfacl -R -m "u:www-data:rwx" /srv/http/

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 uses WP-CLI to update WordPress core, themes and plugins automatically. I found this way is more stable than using add_filter functions to auto update WordPress. Create a .sh file in root user’s home directory.

sudo nano /root/

Put the following lines into the file. You may also want to auto update WP-CLI itself, so add wp cli update to this file.

#! /bin/bash
cd /var/www/wp-site/
sudo -u www-data wp core update --quiet
sudo -u www-data wp core update-db --quiet
sudo -u www-data wp theme update --all --quiet
sudo -u www-data wp plugin update --all --quiet
wp cli update --quiet --yes

Note that I use sudo -u to change the user. Many people think that the su in sudo means super user . Actually it means “switch user and do something”. By default it switch to root. The --quiet flag will suppress informational messages and the --yes flag will provider a yes answer to question if a new stable version of WP CLI is avaiable.

Save the script and add executable permission.

sudo chmod +x /root/

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 * * * bash /root/

It is a good idea to set the PATH variable so cron can find the commands you want to run. Put the following line at the beginning of crontab file.


How to Disable WordPress Update Notification in Dashboard

If you have configured auto update in Cron, you may want to disable the update notification in WordPress dashboard. Simply add the following code in the functions.php file under your theme folder and you are done.

//disable WordPress core update notification
function remove_core_updates()
 if(! current_user_can('update_core')){return;}
 add_action('init', create_function('$a',"remove_action( 'init', 'wp_version_check' );"),2);

Search & Replace with WP CLI

You can use WP CLI to batch replace a text pattern in the WordPress database. For example, if you migrated WordPress from HTTP to HTTPS, then you might want to replace all HTTP links with HTTPS by running the following command.

wp search-replace '' '' --precise --recurse-objects --all-tables

Need help?

Do you need help setting up this on your own service?
Please contact us and we’ll provide you the best possible quote!