WP-CLI is used to manage WordPress installations with a set of command-line commands. Although WordPress is well known for the easy to use installation and user interface, some people still prefer managing it with a command-line tool. WP-CLI is the most commonly used command-line tool made for WordPress and it is easy to learn and use. Let’s start off with a little introduction to WP-CLI first.
What can WP-CLI do?
WP-CLI has an extensive amount of features and capabilities, but listed below are one of the most commonly used:
- Install and update WordPress
- Install and update WordPress plugins
- Install and update WordPress themes
- Managing WordPress files
- Managing WordPress databases
- Adding new posts on a WordPress based website and more…
You can do just about everything from WP-CLI and it is all from a central space. You don’t need to go through pages or dashboard to change something in your WordPress installation.
The most common use of WP-CLI is on a Linux VPS. If you are familiar with the Linux command line interface then you should have no problem adapting to WP-CLI. For the purposes of this tutorial, we will be using a CentOS VPS from RoseHosting but you can use the same instructions on any other virtual server or dedicated server using Linux.
At the time of writing this tutorial, the latest stable version of WP-CLI is and it requires:
- PHP 5.3.x or newer
- WordPress 3.7 version or newer
- Linux, OS X or FreeBSD installed on your server/local machine.
The first step is to download the wp-cli.phar file, save it as ‘wp’ to a directory on your server, so log in to your server via SSH and run:
curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar --output wp
Make the file executable and move it to the /usr/bin directory on your server:
# chmod 755 wp # mv wp /usr/bin/
In order to test and see if wp application is within the path, run the following command:
# which wp
The output of the command should look like this:
It is recommended to change from root to a user:
# su – username
username is the name of the actual user account under which you want to install WordPress.
Before continuing with WP-CLI, you can run the help command which will give you a list of available commands and a description for each one:
$ wp --help | less NAME wp DESCRIPTION Manage WordPress through the command-line. SYNOPSIS wp
SUBCOMMANDS cache Manage the object cache. cap Manage user capabilities. cli Get information about WP-CLI itself. comment Manage comments. core Download, install, update and otherwise manage WordPress proper. cron Manage WP-Cron events and schedules. db Perform basic database operations. eval Execute arbitrary PHP code. eval-file Load and execute a PHP file. export Export content to a WXR file. help Get help on WP-CLI, or on a specific. command. import Import content from a WXR file. media Manage attachments. menu List, create, assign, and delete menus network option Manage options. package Manage WP-CLI packages. plugin Manage plugins. post Manage posts. post-type Manage post types. rewrite Manage rewrite rules. role Manage user roles. scaffold Generate code for post types, taxonomies, etc. search-replace Search/replace strings in the database. server Launch PHP's built-in web server for this specific WordPress installation. shell Interactive PHP console. sidebar Manage sidebars. site Perform site-wide operations. super-admin List, add, and remove super admins from a network. taxonomy Manage taxonomies. term Manage terms. theme Manage themes. transient Manage transients. user Manage users. widget Manage sidebar widgets. GLOBAL PARAMETERS --path= Path to the WordPress files. --url= Pretend request came from given URL. In multisite, this argument is how the target site is specified. --user=
If you go over the commands you’ll know their purpose and how to use them.
Install WordPress using WP-CLI
Although you can still install WordPress through the web browser GUI, we will show you how to install it through command line using WP-CLI:
$ cd /home/username/public_html
username with the actual username.
$ mkdir -p your-website-name.com $ cd your-website-name.com
Change ‘yourwebsitename.com’ with the actual domain name you intend to use with your WordPress installation.
You can start by downloading the latest version of WordPress using the command below:
$ wp core download Downloading WordPress 4.5 (en_US)... md5 hash verified: 6beda5bee679ddff61cb8e2e163f23bf Success: WordPress downloaded.
Create a new MySQL database
WordPress requires a database to work as this is where data is saved, so create a new MySQL database on your server:
$ sudo mysql -u root -p mysql> create database wpdatabase; Query OK, 1 row affected (0.00 sec) mysql> create user someuser@localhost identified by 'your-password'; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on wpdatabase.* to someuser@localhost identified by 'your-password'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye
Where: wpdatabase is the name of the WordPress database, someuser and “your-password” are the MySQL username and password respectively, so change them and make sure to use a strong password.
Generate the wp-config.php WordPress configuration file:
After creating a database, you need to generate the wp-config.php file in order to configure your WordPress installation with your database and server information.
$ wp core config --dbname=wpdatabase --dbuser=someuser --dbpass=your-password Success: Generated wp-config.php file.
Then, install WordPress using the following command:
$ wp core install --url="your-website-name.com" --title="Blog Title" --admin_user="admin_username" --admin_password="your_admin_password" –admin_email="email@example.com" Success: WordPress installed successfully.
Do not forget to change
Install a new theme with WP-CLI
The first thing to do after installing WordPress is to choose a theme for your new website. You can find a theme using the wp theme search command, for example:
$ wp theme search evolve Success: Showing 2 of 2 themes. +--------------+--------------+--------+ | name | slug | rating | +--------------+--------------+--------+ | evolve | evolve | 86 | | Mobile First | mobile-first | 0 | +--------------+--------------+--------+
Install and activate the evolve theme using the following commands:
$ wp theme install evolve Installing evolve (3.5.0) Downloading install package from https://downloads.wordpress.org/theme/evolve.3.5.0.zip... Unpacking the package... Installing the theme... Theme installed successfully. $ wp theme activate evolve Success: Switched to 'evolve' theme.
Or, you can install it directly by downloading a theme and extracting it to the
/home/username/public_html/your-website-name.com/wp-content/themes directory on your server. After that, you can install and activate it by using the wp theme activate theme_name command.
To get a list of all installed WordPress themes, run the following command:
$ wp theme list +----------------+----------+--------+---------+ | name | status | update | version | +----------------+----------+--------+---------+ | evolve | active | none | 3.5.0 | | twentyfifteen | inactive | none | 1.5 | | twentyfourteen | inactive | none | 1.7 | | twentysixteen | inactive | none | 1.2 | +----------------+----------+--------+---------+
To delete a theme, use the following command:
$ wp theme delete twentyfifteen Success: Deleted 'twentyfifteen' theme.
Install plugins using WP-CLI
Everyone uses many different plugins with their WordPress installations. Luckily, you can install and manage plugins with WP-CLI. First off, you need to find a plugin you need to install:
$ wp plugin search "DB - Optimize" Success: Showing 10 of 502 plugins. +----------------------------+---------------------------+--------+ | name | slug | rating | +----------------------------+---------------------------+--------+ | DB - Optimize | db-optimize | 100 | | WP DB Cleaner | wp-db-cleaner | 0 | | GAM DB Backup | gam-db-backup | 0 | | OB Contact Form to DB | ob-contact-form-to-db | 100 | | Oviex Contact Form to DB | oviex-contact-form-to-db | 0 | | Export the DB to file | export-the-db-to-file | 0 | | WP Migrate DB | wp-migrate-db | 96 | | WP-DB-Table-Editor | wp-db-table-editor | 92 | | DB Error Customizer - Free | db-error-customizer | 100 | | Link Data From Another DB | link-data-from-another-db | 100 | +----------------------------+---------------------------+--------+
To install a plugin, for example db-optimize, you can use the following command:
$ wp plugin install db-optimize Installing DB - Optimize (1.1) Downloading install package from https://downloads.wordpress.org/plugin/db-optimize.1.1.zip... Unpacking the package... Installing the plugin... Plugin installed successfully.
To update a single plugin, you need to enter a the name of the WordPress plugin:
$ wp plugin update db-optimize
To update all of the WordPress plugins, you need to enter:
$ wp plugin update -all
Update WordPress with WP-CLI
Your site will be very vulnerable to attacks if you don’t update your WordPress core, themes and plugins whenever a new version come out. You can update WordPress by entering the following commands:
$ wp core update Success: WordPress is up to date. $ wp core update-db Success: WordPress database already at latest db version 36686
Manage your database with WP-CLI
Managing your database is possible with WP-CLI as well as using a third-party editor like phpmyadmin.
To connect to the MySQL console using the WordPress credentials, use the following command:
$ wp db cli
To create a MySQL dump of the WordPress database, run the following command:
$ wp db dump Success: Exported to wpdatabase.sql
Now you know how easy and fun it is to work with WP-CLI. Explore the capabilities of it more and you will learn a lot for both WordPress and the command-line interface. You can use a combination of the GUI back-end editor and WP-CLI too. If you run into any problems with your WordPress website, be sure to contact your support. Some companies like RoseHosting offer free 24/7 managed support and they can help you with any problem related to WordPress and WP-CLI.