WP-CLI is a command line tool for interacting with and managing WordPress sites. WP-CLI is very similar in functionality to what drush provides Drupal.

The current stable release is version 2.0.1. For announcements, follow @wpcli on Twitter or sign up for email updates. Check out the roadmap for an overview of what’s planned for upcoming releases.

In this tutorial we’ll learn how to install wp-cli on a server and some basics. With WP-CLI you can speed up common WordPress maintenance, automate tasks etc.

Installing wp-cli for All Users

Installing a tool like wp-cli on a server globally means that any user will be able to use the application. With WordPress being one of the most common CMS’ it’s helpful to have wp-cli installed for all users.

Pre-flight Check:
Before installing WP-CLI, please make sure your environment meets the minimum requirements:
1. UNIX-like environment (OS X, Linux, FreeBSD, Cygwin); limited support in Windows environment.
2. PHP 5.4, or higher, will be required for wp-cli to function.
3. WordPress 3.7 or later. Versions older than the latest WordPress release may have degraded functionality.

Once you’ve verified requirements, download the wp-cli.phar  file using wget or curl:

/curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Next, check the Phar file to verify that it’s working:

php wp-cli.phar –info

To use WP-CLI from the command line by typing wp, make the file executable and move it to somewhere in your PATH. For example:

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

If WP-CLI was installed successfully, you should see something like below:

[root@server ~]# wp –info
OS: Linux server.linux.com 3.10.0-514.10.2.el7.centos.plus.x86_64 #1 SMP Fri Mar 3 02:04:03 UTC 2017 x86_64
Shell: /bin/bash
PHP binary: /opt/cpanel/ea-php56/root/usr/bin/php
PHP version: 5.6.37
php.ini used: /opt/cpanel/ea-php56/root/etc/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: /root
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.0.1

Updating wp-cli

You can update WP-CLI with wp cli update, or by repeating the installation steps.

Verify & Test wp-cli as a Site User

Now that wp-cli is installed globally you will want to test the tool from a user hosting a WordPress site. To do this you will log in as root via SSH then switch to a normal user (SSH access should be enabled for the user).

$ su – wordpress

By executing this command you will then be logged in as the ‘wordpress’ user. Do take note that you’ll enter the actual username where it says ‘wordpress’.

First, let’s get the current version of WP-CLI we’re running.

$ wp –version

Manage WordPress Plugins & Themes

WP-CLI can be used to manage WordPress plugins from the command line. This is useful for scripting or automating the management of your plugins. To get information about all the WP-CLI commands related to plugins, run the following

$ wp help plugin

NAME

wp plugin

DESCRIPTION

Manages plugins, including installs, activations, and updates.

SYNOPSIS

wp plugin <command>

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.

EXAMPLES

# List plugins

[wordpress@server public_html]$ wp plugin list
+———+———-+———–+———+
| name | status | update | version |
+———+———-+———–+———+
| akismet | inactive | available | 4.0.3 |
| jetpack | inactive | none | 6.5 |
+———+———-+———–+———+

# Activate plugin

[wordpress@server public_html]$ wp plugin activate hello
Plugin ‘hello’ activated.
Success: Activated 1 of 1 plugins.

# Deactivate plugin

[wordpress@server public_html]$ wp plugin deactivate hello
Plugin ‘hello’ deactivated.
Success: Deactivated 1 of 1 plugins.

# Delete plugin

[wordpress@server public_html]$ wp plugin delete hello
Deleted ‘hello’ plugin.
Success: Deleted 1 of 1 plugins.

# Upgrade all installed plugins at once

[wordpress@server public_html]$ wp plugin update –all
Downloading update from https://downloads.wordpress.org/plugin/akismet.4.0.8.zip…
Unpacking the update…
Installing the latest version…
Removing the old version of the plugin…
Plugin updated successfully.
+———+————-+————-+———+
| name | old_version | new_version | status |
+———+————-+————-+———+
| akismet | 4.0.3 | 4.0.8 | Updated |
+———+————-+————-+———+

Success: Updated 1 of 1 plugins.

If you wish to only upgrade a single plugin for some reason, you can do that with the command:

$ wp plugin update <plugin_name>

WP-CLI can be used to manage themes in a similar way it can be used to manage plugins – it can install, enable, update, and much more, all from the command line. To get information about all the WP-CLI commands related to themes, run the following:

$ wp help theme

NAME

wp theme

DESCRIPTION

Manages themes, including installs, activations, and updates.

SYNOPSIS

wp theme <command>

SUBCOMMANDS

activate Activates a theme.
delete Deletes one or more themes.
disable Disables a theme on a WordPress multisite install.
enable Enables a theme on a WordPress multisite install.
get Gets details about a theme.
install Installs one or more themes.
is-active Checks if a given theme is active.
is-installed Checks if a given theme is installed.
list Gets a list of themes.
mod Sets, gets, and removes theme mods.
path Gets the path to a theme or to the theme directory.
search Searches the WordPress.org theme directory.
status Reveals the status of one or all themes.
update Updates one or more themes.

How about a list of all the themes currently installed?
wp theme list

EXAMPLES
#List themes

[wordpress@server public_html]$ wp theme list
+—————–+———-+———–+———+
| name | status | update | version |
+—————–+———-+———–+———+
| twentyfifteen | inactive | none | 2.0 |
| twentyseventeen | active | available | 1.6 |
+—————–+———-+———–+———+

#Install a theme

[wordpress@server public_html]$ wp theme install twentysixteen
Installing Twenty Sixteen (1.5)
Downloading installation package from http://downloads.wordpress.org/theme/twentysixteen.1.5.zip…
Unpacking the package…
Installing the theme…
Theme installed successfully.
Success: Installed 1 of 1 themes.

#Activate a theme

[wordpress@server public_html]$ wp theme activate twentysixteen
Success: Switched to ‘Twenty Sixteen’ theme.

Upgrade WordPress Core from the Command Line with WP-CLI

Get started by checking the version of WordPress you’re running:
$ wp core version

[wordpress@server public_html]$ wp core version 4.9.7

The WordPress community releases new versions many times a year, minor versions to fix bugs and vulnerabilities, and major versions when new features are added. Chances are, you need to upgrade the core WordPress files to enjoy performance enhancements, security fixes, and new features. The classic way is using the WordPress admin dashboard which requires you to start your browser and click around your site a couple of times. WP-CLI simplifies this task immensely:

$ wp core update

This will check for a newer version of WordPress, and install it if there is one.

[wordpress@server public_html]$ wp core update
Updating to version 4.9.8 (en_US)…
Downloading update from https://downloads.wordpress.org/release/wordpress-4.9.8-no-content.zip…
Unpacking the update…
Success: WordPress updated successfully.

If you need to downgrade WordPress for some reason, WP-CLI can do this as well. The following command will downgrade WordPress core to a version you specify:

$ wp core update –version=VERSION_NUMBER –force

The “–force” flag is required, as it is not recommended that you downgrade WordPress.

Manage WordPress Users

List Existing Users

You can enter the below command to see the list of all your WordPress users:

$ wp user list

The output will look similar to the following:

[wordpress@server public_html]$ wp user list
+—-+————+————–+—————+———————+—————+
| ID | user_login | display_name | user_email | user_registered | roles |
+—-+————+————–+—————+———————+—————+
| 1 | admin | admin | admin@wordpresstest.com | 2018-07-16 10:14:15 | administrator |
+—-+————+————–+—————+———————+—————+

This summarizes the information about your users: their usernames to log in to your site’s WordPress Dashboard, the names displayed on their posts, the dates they registered as users, and their roles.

Create a New User

To create a new administrator user, enter the below command, replacing “NEWUSERNAME” and “EMAILADDRESS” with your user’s information:

$ wp user create NEWUSERNAME EMAILADDRESS –role=administrator

[wordpress@server public_html]$ wp user create newadmnusr
adm@wordpresstest.com –role=administrator
Success: Created user 2.
Password: 4boBRcZscub9lMU4$HfL^yvS

You can set the password with the –user_pass command,replacing “MySecurePassword” with your password:
$ wp user create NEWUSERNAME EMAILADDRESS –role=administrator –user_pass=MySecurePassword

Change a User’s Password

To change a user password, enter the below command :
$ wp user update USERNAME –user_pass=MyNewSecurePassword

 

[wordpress@server public_html]$ wp user update newadmnusr –user_pass=’87gt[fdsd’
Success: Updated user 2.

Remove a User
If you wish to remove a user that does not have any posts you want to preserve, you can do so with this command, replacing “USERNAME” with the user:
$ wp user delete USERNAME
You’ll receive a prompt that the system will delete any posts for this user; press y if you wish to proceed.
If the user has posts you would like to save, you can select another user to assign them to by entering this command and substituting the appropriate usernames for the fields shown in ALL CAPS:
$ wp user delete USERNAME –reassign=ANOTHERUSER
That’s it for our basic introduction to the WP-CLI. Below is a list of resources if you want to dig a little deeper and expand your knowledge.
Official WP-CLI Handbook

WP-CLI Commands

WP-CLI Community Commands

Common
Issues

[tagline_box backgroundcolor=”description=” shadow=”no” shadowopacity=”0.7″ border=”1px” bordercolor=”” highlightposition=”top” content_alignment=”left” link=”” linktarget=”_self” modal=”” button_size=”” button_shape=”” button_type=”” buttoncolor=”” button=”” title=”” description=”If you have any queries on installation of wp-cli on a server free to leave us a message and our representative will get back to you.

” margin_top=”50px” margin_bottom=”” animation_type=”slide” animation_direction=”left” animation_speed=”0.3″ class=”” id=””]

    [/tagline_box]