WP-Cli is a command line interface which allows the users to manage their WordPress web sites from the command prompt. Upgrades can be performed, backups can be generated, new posts can be published and most of the regular admin actions can be performed with a set of commands.

In this tutorial we will explain how to use the WP command line interface in order to complete regular administrative tasks like upgrades, database backup creation, plugins and themes installations and removals, publishing and deleting posts, changing site’s URL settings and getting help on chosen commands. Note that WP-Cli requires an SSH access.

Table of Contents

List the Available WP-Cli Commands
Update WordPress via WP-Cli
Update WordPress Plugins using WP-Cli
Install and Activate WordPress Plugins & Themes via WP-Cli
Other Tasks you Can do With WP-Cli

List the Available WP-Cli Commands

divider

First, we will start with the instructions on how to list the available commands and get detailed help for each one of them.

To list all the commands write wp help or just wp.

The following list will be shown:

Available commands:wp core [download|config|install|install_network|version|update|update_db]
wp db [create|drop|optimize|repair|connect|cli|query|export|import]
wp eval-file
wp eval
wp export [validate_arguments]
wp generate [posts|users]
wp home
wp option [add|update|delete|get]
wp plugin [activate|deactivate|toggle|path|update|uninstall|delete|status|install]
wp post-meta [get|delete|add|update]
wp post [create|update|delete]
wp theme [activate|path|delete|status|install|update]
wp transient [get|set|delete|type]
wp user-meta [get|delete|add|update]
wp user [list|delete|create|update]

See ‘wp help ‘ for more information on a specific command.

Global parameters:
--user=<id|login>set the current user
--url=set the current URL
--path=set the current path to the WP install
--require=load a certain file before running the command
--versionprint WP-Cli version

If you want to get detailed documentation, for example, for the wp db export command, enter the code listed below:

wp help db export

The result will be as follows:

WP-DB-EXPORT(1) WP-Cli WP-DB-EXPORT(1)
NAME
wp-db-export - Export the WordPress database using mysqldump.
SYNOPSIS
wp db export [file]
OPTIONS
file: The name of the export file. If omitted, it will be ´{dbname}.sql´

Now we will proceed with the regular management tasks.

Update WordPress via WP-Cli

divider

First, we will show how to update the core files of your web site. We will check the current version which we have:

wp core version
3.4.2

Obviously it is outdated. In order to use the latest scripts features and have all the vulnerabilities in the older versions patched we should always use the latest stable releases for our web site applications. Using WP-Cli we will complete the upgrade with a single command:

wp core update
Downloading update from http://wordpress.org/wordpress-3.5.1-new-bundled.zip...
Unpacking the update...
Success: WordPress updated successfully.

Update WordPress Plugins using WP-Cli

divider

The update of the core files and the database are not always enough. If we have integrated external plugins and/or themes we should take care of their upgrade. We need to check the status of the installed extensions:

wp plugin status

Installed plugins:
UI akismet
I hello
UA jetpack
A limit-login-attempts
A SGCachePress

Legend: I = Inactive, A = Active, M = Must Use, U = Update Available

We have 5 plugins integrated in our sample installation. There are upgrades for two of them. We will complete the upgrades through the following commands:

wp plugin update jetpack

Downloading update from http://downloads.wordpress.org/plugin/jetpack.2.3.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
wp plugin update akismet

Downloading update from http://downloads.wordpress.org/plugin/akismet.2.5.8.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.

Install and Activate WordPress Plugins & Themes via WP-Cli

divider

To install and activate a new plugin run the commands listed below:

  • wp plugin install Plugin_Name
  • wp plugin activate Plugin_Name

You should replace the “Plugin_Name” string with the exact plugin name.

If you do not need a plugin you can remove it through the following action:

wp plugin uninstall Plugin_Name

Next, we will check the themes:

wp theme status
Installed themes:
UA twentyeleven
UI twentyten
I twentytwelve

Legend: I = Inactive, A = Active, U = Update Available

To update all the themes the following command should be executed:

wp theme update --all 

Enabling Maintenance mode...
Downloading update from http://wordpress.org/themes/download/twentyeleven.1.5.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the theme...
Theme updated successfully.
Downloading update from http://wordpress.org/themes/download/twentyten.1.5.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the theme...
Theme updated successfully.
Disabling Maintenance mode...
Success: Updated 2/2 themes.

The themes can be installed, activated and uninstalled in a similar way to the plugins:

  • wp theme install Theme_Name
  • wp theme activate Theme_Name
  • wp theme delete Theme_Name

The “Theme_Name” string should be replaced with the actual theme name.

The activation will make the chosen theme the default one for your WordPress installation.

Other Tasks you Can do With WP-Cli

divider

We can even write posts directly from the command line. The creation of a sample post on a new page is illustrated below:

wp post create --post_type=page --post_status=publish --post_title='My test post' --post_content='This is a test post'Success: Created post 4.

If we do not need a certain post we can easily delete it:

wp post delete 3 --forceSuccess: Deleted post 3.

Once we have our script upgraded and the content set we can generate a database backup:

wp db exportSuccess: Exported to user_wp790.sql

If the web master decides to change the primary domain for the WordPress web site he/she can easily update the corresponding URL options:

wp option update home https://newdomain.com
wp option update siteurl https://newdomain.com

More details on WP cli can be found through the help command and the project’s home page.