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

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 the 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 wget.

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:

OS:	Linux 5.15.0-37-generic #39-Ubuntu SMP Wed Jun 1 19:16:45 UTC 2022 x86_64
Shell:	/bin/bash
PHP binary:	/usr/bin/php8.0
PHP version:	8.0.20
php.ini used:	/etc/php/8.0/cli/php.ini
MySQL binary:	/usr/bin/mysql
MySQL version:	mysql  Ver 15.1 Distrib 10.6.7-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper
SQL modes:	
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.6.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, like so:

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 a 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.

Note: If you see the following error

PHP Fatal error: Uncaught Error: Call to undefined function add_filter()

Then you may need to comment out the add_filter function in wp-config.php file for WP-CLI to work.

Managing WordPress Core

Check WordPress core version.

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. (Note: This command will temporarily make your website inaccessible: 404 not found.)

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 the 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 updates 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

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 /var/www/ 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" /var/www/

If WP-CLI can’t uninstall a plugin, then you can just go to the wp-content/plugins/ directory and delete that plugin’s folder.

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 back up 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/auto-update-wp.sh

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

exit

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 switches to root. The --quiet flag will suppress informational messages and the --yes flag will provide a yes answer to question if a new stable version of WP CLI is available.

You might want to change

sudo -u www-data wp core update --quiet

to

sudo -u www-data wp core update --minor --quiet

so WordPress will automatically update to the latest minor version, instead of the latest main version, which is likely to introduce bugs. I always configure my WordPress to stay one version behind. For example, if the latest main version is 6.0, I make my WordPress stay at version 5.9. When WordPress 6.1 is out, I can update my WordPress to 6.0 with:

sudo -u www-data wp core update --version=6.0

Then update to the latest minor version.

sudo -u www-data wp core update --minor

You can check the WordPress versions page to know the current versions.

Save the script and add executable permission.

sudo chmod +x /root/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 * * * bash /root/auto-update-wp.sh

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.

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

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
add_action('after_setup_theme','remove_core_updates');
function remove_core_updates()
{
 if(! current_user_can('update_core')){return;}
 add_action('init', create_function('$a',"remove_action( 'init', 'wp_version_check' );"),2);
 add_filter('pre_option_update_core','__return_null');
 add_filter('pre_site_transient_update_core','__return_null');
}

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 'http://yourdomain.com' 'https://yourdomain.com' --precise --recurse-objects --all-tables

Update

Starting from version 5.5, WordPress allows you to set plugins and themes to update automatically or not in the WordPress admin interface.

Wrapping Up

This is just an introduction. WP-CLI is very extensible and can do many more things. You may also want to set up the ModSecurity web application firewall to protect your WordPress site from hacking. If you use Apache web server on Debian/Ubuntu, then read the following tutorial.

If you use Nginx web server on Debian/Ubuntu, then read the following tutorial:

As always, if you found this post useful, then subscribe to our free newsletter to get more tutorials 🙂

Rate this tutorial
[Total: 10 Average: 4.8]

9 Responses to “Use WP-CLI To Manage Your WordPress Site From Command Line

  • I have installed wordpress using cyberpanel,
    the WordPress installation directory is in home directory
    I can not use the wp command, any help?

  • Awesome tutorial, I was just wondering to try wp cli and it seems awesome just other great frameworks offer cli environment. Now with this wp cli we can speed up the wordpress website management.

  • If the PermitRootLogin is set to no in /etc/ssh/sshd_config then, we cannot create a .sh file in the root directory. Alternatively, I have followed the following steps to use another user with SUDO power like jiwe in my case:
    Create a .sh file in root user’s home directory.

    sudo nano /home/jiwe/auto-update-wp.sh
    

    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/domain.com/html/wordpress
    sudo -u www-data wp core update --minor --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
    exit
    
    sudo chmod +x /home/jiwe/auto-update-wp.sh
    

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

    sudo crontab -e
    

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

    0 4 * * * bash /home/jiwe/auto-update-wp.sh
    

    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.

    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    

    Is what I have done correct or I should have followed the steps in your tutorial?

    • Xiao Guoan (Admin)
      3 years ago

      Your steps are correct. In my case, the PermitRootLogin is set to no in /etc/ssh/sshd_config and I can still create a .sh file in /root/ directory. But you can create the .sh file in any directory you have permission on.

  • Hello,
    I have followed this tutorial but I have done a mistake that I cannot identify. Because none of the websites that I have applied this guidance on them got updated. Those which I have not followed this guidance on them got updated. I have the following in the PHP error messages:

    [10-Mar-2021 06:00:06 UTC] PHP Warning:  Trying to access array offset on value of type null in phar:///usr/local/bin/wp/vendor/wp-cli/extension-command/src/Theme_Command.php on line 472
    [10-Mar-2021 06:00:06 UTC] PHP Warning:  Trying to access array offset on value of type null in phar:///usr/local/bin/wp/vendor/wp-cli/extension-command/src/Theme_Command.php on line 473
    [10-Mar-2021 06:00:06 UTC] PHP Warning:  Trying to access array offset on value of type null in phar:///usr/local/bin/wp/vendor/wp-cli/extension-command/src/Plugin_Command.php on line 662
    [10-Mar-2021 06:00:06 UTC] PHP Warning:  Trying to access array offset on value of type null in phar:///usr/local/bin/wp/vendor/wp-cli/extension-command/src/Plugin_Command.php on line 663
    

    If you can help me troubleshoot the issue, I will highly appreciate it.

  • Update:
    To test if the commands that I posted in the #! /bin/bash work in the first place, I have run them raw as follows:

    cd /var/www/example.com/html/wordpress
    sudo -u www-data wp core update --minor --quiet
    sudo -u www-data wp core update-db --quiet
    

    All commands returned with errors around this:

    Warning: Trying to access array offset on value of type null in phar:///usr/local/bin/wp/vendor/wp-cli/extension-command/src/Theme_Command.php on line 473
    PHP Warning:  Trying to access array offset on value of type null in phar:///usr/local/bin/wp/vendor/wp-cli/extension-command/src/Theme_Command.php on line 472
    

    The lines are long but I don’t want to mess up the post here. Upon further investigating the issue, I found out that what we see here is an issue with the current stable version (2.4.0). The issue though has been fixed already but there’s no new stable version yet. To verify this I run wp cli update –nightly which installed a snapshot of the upcoming version (2.5.0). When I run the same commands, there were no errors. The issue was discussed in further details on Github: https://github.com/wp-cli/wp-cli/issues/5494

  • This command ends with error:

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

    I don’t have

    /srv/http/

    directory. Any suggestion this should be replaced with what?

    • Xiao Guoan (Admin)
      3 years ago

      You can use this command instead.

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

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here