WordPress wp-cli overview

Overview

WordPress Command Line Interface (wp-cli ) is a command-line WordPress tool installed on all DreamHost servers. wp-cli allows you to run WordPress commands from your Shell terminal to manage your WordPress site without the need to log in to the WordPress dashboard.

You will need to log in to your server via SSH to use wp-cli. View the following articles for information on how to create a Shell user and log into your server.

After you're logged in to your server, make sure you’re in your website’s directory. This is where you'll run all commands in this article.

Command examples in this article

 

General

These are a few general commands to get you started.

View all wp-cli commands

[server]$ wp

A list of all available commands displays.

Using --help to view command information

To get more information on a specific command, run the --help option.

[server]$ wp plugin --help

This prints a list of available subcommands. For example:

SUBCOMMANDS
  
    activate              Activates one or more plugins.
    deactivate            Deactivates one or more plugins.
    delete                Deletes plugin files without deactivating or uninstalling.
    get                   Gets details about an installed plugin.
    install               Installs one or more plugins.
    is-active             Checks if a given plugin is active.
    is-installed          Checks if a given plugin is installed.
    list                  Gets a list of plugins.
    path                  Gets the path to a plugin or to the plugin directory.
    search                Searches the WordPress.org plugin directory.
    status                Reveals the status of one or all plugins.
    toggle                Toggles a plugin's activation state.
    uninstall             Uninstalls one or more plugins.
    update                Updates one or more plugins.
    verify-checksums      Verifies plugin files against WordPress.org's checksums.  

You can then add one of these subcommands to the main command to perform an action. Here is how you’d install a plugin named hello-dolly:

[server]$ wp plugin install hello-dolly
  Installing Hello Dolly (1.6)
  Downloading install package from https://downloads.wordpress.org/plugin/hello-dolly.1.6.zip...
  Unpacking the package...
  Installing the plugin...
  Plugin installed successfully.  

wp cli version

Action Command

View the wp-cli version

[server]$ wp cli version

Check for updates

[server]$ wp cli check-update

Update wp-cli

[server]$ wp cli update

 

Top commands

These commands are the most commonly used.

Plugins

Obtaining the correct name of the plugin

The name you use when entering the PLUGIN_NAME must be the name from the plugin site's web page. For example, if you wanted to install W3 Total Cache you'd visit this page:

You can see it ends with w3-total-cache. This is the plugin name you would enter in the commands below.

Action Command

View installed plugins

[server]$ wp plugin list

Install a plugin

[server]$ wp plugin install PLUGIN_NAME

Activate a plugin

[server]$ wp plugin activate PLUGIN_NAME

Deactivate a plugin

[server]$ wp plugin deactivate PLUGIN_NAME
-or-
[server]$ wp plugin toggle PLUGIN_NAME

Themes

Action Command

View installed themes

[server]$ wp theme list

Install a theme

[server]$ wp theme install THEME_NAME

Activate a different theme

[server]$ wp theme activate THEME_NAME

Update a theme

[server]$ wp theme update THEME_NAME

Updating WordPress

Action Command
View the current WordPress version
[server]$ wp core version
Check for WordPress version updates
[server]$ wp core check-update
Update WordPress to a newer version
[server]$ wp core update

Force download all core files.This forces the core WordPress files to be re-downloaded into your current directory and overwrite any core files that already exist.

[server]$ wp core download --force

Users

Creating users

Action Command
View a list of all current users
[server]$ wp user list

Create a new username while assigning its email and role. A new password is printed to the terminal after the user is created.

[server]$ wp user create newusername admin@example.com --role=administrator

Changing a password

Action Command
Run wp user list to obtain a user's ID#. Use that user ID# to update their password. This example is changing a user with an ID# of 3. You would then need to inform the user of their password change.
[server]$ wp user update 3 --user_pass=NEW_PASSWORD

Deleting a user

Action Command
Run wp user list to obtain the ID# of the user you wish to delete and the user you wish to reassign its posts. You'll need to to reassign any posts to another user so they are not permanently deleted. This example deletes a user with an ID# of 13 and reassigns their posts to user 7.
[server]$ wp user delete 13 --reassign=7

 

Troubleshooting

Cache

Action Command
Flush your site's cache
[server]$ wp cache flush
Clear the wp-cli cache
[server]$ wp cli cache clear

Database

Action Command
Check the status of the database
[server]$ wp db check
Optimize the database
[server]$ wp db optimize
Repair the database
[server]$ wp db repair
Search for text in the database.
This searches for the word hello
[server]$ wp db search hello
Upgrade the database version
[server]$ wp core update-db

Permalinks

Action Command
Flush permalinks. This is the same as re-saving them in the dashboard.
[server]$  wp rewrite flush

 

Custom changes

Updating the home and siteurl

Action Command
View the home and siteurl
[server]$  wp option list | grep -E "^(siteurl|home)"
Update the home url
[server]$ wp option update home https://example.com
Update the siteurl
[server]$ wp option update siteurl https://example.com

Importing and exporting a WXR file

You can use the import and export commands to manage your site data with a WXR file. View the following article for details.

Search and Replace

Search and replace text in your database. Make sure to use the --dry-run flag the first time to see what will change.

[server]$ wp search-replace http://example.com https://example.com/blog --dry-run

A list of fields that will be updated displays in your terminal. If you'd like to update all output displayed, run the command again without the --dry-run flag.

[server]$ wp search-replace http://example.com https://example.com/blog

wp-config.php

Action Command
View all settings in your wp-config.php file
[server]$ wp config list
Update a setting in your wp-config.php file.
This updates the DB_HOST value.
[server]$ wp config set DB_HOST mysql.example.com

Updating the PHP version wp-cli uses

  1. View the current version of PHP and its location by running the following command:
    [server]$ wp cli info
    OS: Linux 4.14.117-grsec-grsec+ #1 SMP Fri May 10 17:15:47 PDT 2019 x86_64
    Shell: /bin/bash
    PHP binary: /usr/local/php74/bin/php
    PHP version: 7.4.15
    php.ini used: /etc/php74/php.ini
    WP-CLI root dir: /usr/local/wp
    WP-CLI vendor dir: /usr/local/wp/vendor
    WP_CLI phar path:
    WP-CLI packages dir:
    WP-CLI global config: /usr/local/wp/config.yml
    WP-CLI project config:
    WP-CLI version: 2.4.0
  2. To change the version of PHP wp-cli uses, add the following line to your .bash_profile. Change the version as needed. This example uses PHP 8.0.
    export WP_CLI_PHP=/usr/local/php80/bin/php wp
  3. Update the .bash_profile:
    [server]$ . ~/.bash_profile
  4. Finally, check to confirm wp-cli is using this new version.
    [server]$ wp cli info
    OS:     Linux 4.14.117-grsec-grsec+ #1 SMP Fri May 10 17:15:47 PDT 2019 x86_64
    Shell:  /bin/bash
    PHP binary:     /usr/local/php80/bin/php
    PHP version:    8.0.2
    php.ini used:   /etc/php80/php.ini
    MySQL binary:   /usr/bin/mysql
    MySQL version:  mysql  Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using  EditLine wrapper
    SQL modes:
    WP-CLI root dir:        /usr/local/wp
    WP-CLI vendor dir:      /usr/local/wp/vendor
    WP_CLI phar path:
    WP-CLI packages dir:
    WP-CLI global config:   /usr/local/wp/config.yml
    WP-CLI project config:
    WP-CLI version: 2.5.0-alpha
    

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?