What is the WordPress CLI?
WordPress CLI is the command line interface for WordPress. This allows you to manage numerous different aspects of your WordPress website without logging in directly to the WordPress dashboard.
Examples of the tasks that can be accomplished include managing plugins, themes, users and roles.
There is no need to install WP-CLI, this comes preinstalled with every WordPress hosting package automatically.
Connecting to the package via SSH
Before obtaining access to WP-CLI, you will first need to connect to the hosting package via SSH. To see how to connect to your hosting package via SSH, please see our guide How do I connect a website via SSH?
Listing plugins
To list all of the plugins installed, the core command is wp plugin list
. You can specify further, and add the --status=active
argument, which only lists plugins with the status 'active'.
-bash-4.2$ wp plugin list --status=active
+-----------------+--------+--------+---------+
| name | status | update | version |
+-----------------+--------+--------+---------+
| bbpress | active | none | 2.6.11 |
| buddypress | active | none | 14.0.0 |
| classic-editor | active | none | 1.6.4 |
| classic-widgets | active | none | 0.3 |
| jetpack | active | none | 13.7 |
| performance-lab | active | none | 3.3.1 |
+-----------------+--------+--------+---------+
You can take this one step further by formatting the results as a count and totalling the number of active plugins using the --format=count
argument.
$ wp plugin list --status=active --format=count
6
Installing plugins
Installing plugins with WP-CLI is easy. The only piece of information required is the plugin name. In this example, we're installing the plugin: User Switching.
The core command for installing a plugin is wp plugin install [plugin-name]
. You can add arguments to the end of a command for a further action. In the following example, that is --activate
to automatically activate the plugin as soon as it has finished installing.
$ wp plugin install user-switching --activate
Installing User Switching (1.0.9)
Downloading installation 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.
This saves an additional step of activating the plugin after installation.
Updating plugins
The core command for updating a plugin is wp plugin update [plugin-name]
, however, as with most commands you can add arguments to specify the action. Replacing the plugin name with the --all
argument will update all plugins at once. Other arguments that can be used include: [--exclude=<plugin-name]
, which allows you to exclude a specified plugin from the update or [--minor]
, to only perform 'minor' patches (e.g. from 1.3 to 1.3.3, instead of 1.4).
$ wp plugin update --all --minor
Success: Plugin already updated.
Creating users
The core command for creating new users is wp user create [name] [username] --role=[role-name]
. Similarly to the plugin commands, there are numerous arguments to be added to specify the action further. [--user_pass=<password>]
allows you to set a pre-defined password for the user - by default, a random password is generated.
$ wp user create bob bob@example.com --role=author --user_pass=password123
Success: Created user 3.
Creating user roles
The core command for creating a new user role is wp role create [role-name] [role-group]
. When creating a new role, you must assign the 'role group', which acts as the permission group that will be assigned. In the following example, the role group assigned is 'Approver'.
$ wp role create example-role Approver
Success: Role with key 'example-role' created.
Retrieving site options
The core command is wp option
, with numerous different sub-commands to help add & manage new options for your site. The most common would be to retrieve the site URL. This can be retrieved via the following:
$ wp option get siteurl
https://mydomain-uk.stackstaging.com
More WP-CLI
For a fully comprehensive document on all WP-CLI options, visit https://developer.wordpress.org/cli/commands/