WP-CLI

WP-CLI is a command line interface for WordPress. It allows you to install/update/delete themes and plugins, create and view any data on the site, modify site settings, and much more. All of this is done from the command line without using a browser.

Basic commands:

# Version
wp cli version

# Update
wp cli update --yes

# Update all additional packages
wp package update

Full list: wp cli.

Why do you need WP-CLI?

To speed up and simplify routine actions in the website development process, as well as to provide the ability to manage the website from the command line on a remote server.

WP-CLI can do many things that are usually done through the WordPress admin panel, for example:

  • The command wp plugin install allows you to install and activate a plugin from the WordPress catalog:

    $ wp plugin install user-switching --activate
    
    Installing User Switching (1.0.9)
    Downloading install package from https://downloads.wordpress.org/plugin/user-switching.1.0.9.zip...
    Unpacking the package...
    Installing the plugin...
    Plugin installed successfully.
    Activating 'user-switching'...
    Plugin 'user-switching' activated.
    Success: Installed 1 of 1 plugins.

WP-CLI also includes commands that are not in the WordPress admin panel. For example:

  • With the command wp db export you can export the Database:

    wp db export - | gzip > ./db_backup-$(date +%Y-%m-%d-%H%M%S).sql.gz
  • The command wp cron event list allows you to view all cron events:

    $ wp cron event list
    
    +------------------------------------+---------------------+--------------------+---------------+
    | hook                               | next_run_gmt        | next_run_relative  | recurrence    |
    +------------------------------------+---------------------+--------------------+---------------+
    | action_scheduler_run_queue         | 2023-08-07 15:11:17 | now                | 1 minute      |
    | wp_scheduled_delete                | 2023-08-08 10:49:16 | 1 hour 55 minutes  | 1 day         |
    | delete_expired_transients          | 2023-08-08 10:49:16 | 1 hour 55 minutes  | 1 day         |
    | wp_scheduled_auto_draft_delete     | 2023-08-08 10:59:02 | 2 hours 4 minutes  | 1 day         |
    | wpseo-reindex-links                | 2023-08-08 12:37:07 | 3 hours 43 minutes | 1 day         |
    | wp_site_health_scheduled_check     | 2023-08-08 14:41:17 | 5 hours 47 minutes | 1 week        |
    | wpseo_permalink_structure_check    | 2023-08-08 14:41:17 | 5 hours 47 minutes | 1 day         |
    | wpseo-reindex                      | 2023-08-08 14:42:32 | 5 hours 48 minutes | 1 day         |
    +------------------------------------+---------------------+--------------------+---------------+

WP-CLI Installation

Before installing, make sure that WP-CLI is not already in your environment. To do this, run the following command:

wp --version

If the response shows the version: WP-CLI 2.5.0, then WP-CLI is already installed on your system.

Installation on Linux and Windows WSL

To install WP-CLI, you need to download the package wp-cli.phar. This can be done using the curl or wget utility:

curl -L https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > wp-cli.phar

To check this package, run it with the --info parameter:

php wp-cli.phar --info

You should see the information:

PHP binary:     /usr/bin/php7.4.1
PHP version:    7.4.1
php.ini used:   /home/web/etc/php/php.ini
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar
WP-CLI global config:
WP-CLI version: 2.5.0

To be able to write wp in any directory instead of php wp-cli.phar, you need to make the file executable and copy it to any system directory (you can view system directories with the command echo $PATH).

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/bin/wp

That's it! wp-cli is installed!

Now you can run wp-cli commands from any directory where WP is located.

Read more about installation here:

Installation on Windows

Make sure that php is installed and globally accessible. To do this, run the following command in the console:

php -version

If the command php does not work, you need to install php on your computer and add the folder with the executable file to the PATH environment variable.

Manually download wp-cli.phar (https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar) and save it to a folder, for example, C:\wp-cli

Create a file wp.bat in the C:\wp-cli folder with the following content:

@ECHO OFF
php "c:/wp-cli/wp-cli.phar" %*

Add C:\wp-cli to the path environment variable:

setx path "%path%;c:\wp-cli"

Now you can use WP-CLI from any location in the Windows command line.

List of all commands

See in the "Commands" section.