March 25, 2020
10min Read
Domantas G.

In this article, you are going to learn about WP-CLI (WordPress Command Line Interface). It is a powerful WordPress tool that will improve your productivity and web management skills.
But before we show you how to install and use it, let’s briefly uncover what WP-CLI is.
WP-CLI is a tool that gives you the ability to manage your WordPress site through a command-line interface. You can execute standard functions, such as plugin installation, post creation, WordPress updates, and so on.
Also, it allows you to perform commands that are not supported through the standard WordPress back-end.
The biggest benefit to WP-CLI is that it can save you a lot of time when you’re installing, configuring, or maintaining WordPress websites. If you have multiple sites, you no longer need to log in to each account to take care of basic tasks. What’s great, the WP-CLI commands can be automated, as well.
Sounds interesting, right? So without further ado, here are the steps to install and use WP-CLI.
On Hostinger’s shared hosting, WP-CLI is already installed. Therefore, you only need to connect to your account via SSH in order to use it. However, if you want to install the feature on a VPS or a local Linux system, you can follow the steps below:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
Now that WP-CLI is installed, you can start working with your WordPress content and files through the command line.
WP-CLI is a straightforward tool if you’re already familiar with the command-line environment. Surprisingly, it is often faster than going into the WordPress administration panel and clicking through various options.
By default, WP-CLI comes with numerous built-in commands. Thankfully, you can extend them by scripting your own custom commands or installing plugins that support WP-CLI. If you want to see the list of compatible tools, read this WordPress handbook.
Now, putting that aside, let’s take a look at a few basic WP-CLI commands:
Simply type this command to check the WP-CLI version information:
wp --info
The output should look like this:
PHP binary: /usr/bin/php PHP version: 7.3.11 php.ini used: /opt/alt/php70/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: /Users/test WP-CLI packages dir: WP-CLI global config: WP-CLI project config: WP-CLI version: 2.4.0
Type the following line if you want to see other commands that you can use with WP-CLI:
wp help
The output:
NAME wp DESCRIPTION Manage WordPress through the command-line. SYNOPSIS wp <command> SUBCOMMANDS cache Adds, removes, fetches, and flushes the WP Object Cache object. cap Adds, removes, and lists capabilities of a user role. cli Manage WP-CLI itself. comment Creates, updates, deletes, and moderates comments. core Download, installs, updates and manages a WordPress installation. cron Tests, runs, and deletesq WP-Cron events and schedules. db Perform basic database operations using credentials stored in wp-config.php eval Execute arbitrary PHP code. eval-file Load and execute a PHP file. .....
To exit the help page, press the Q button on your keyboard.
You can also access a separate help page for each command. For example, this is how you see more details about the comment command:
wp help comment
Output:
NAME wp comment DESCRIPTION Manage comments. SYNOPSIS wp comment <command> SUBCOMMANDS approve Approve a comment. count Count comments, on whole blog or on a given post. create Create a new comment. delete Delete a comment. exists Verify whether a comment exists. generate Generate some number of new dummy comments. .....
As you can see from the output, each WP-CLI command has a list of subcommands. Let’s say we want to get the number of available comments. We can use the following argument:
wp comment count
What’s cool, you can go even further to see the help page for a subcommand of a subcommand, like this:
wp help comment count
Output:
NAME wp comment count DESCRIPTION Count comments, on whole blog or on a given post. SYNOPSIS wp comment count [<post-id>] OPTIONS [<post-id>] The ID of the post to count comments in. .....
If it’s the first time you are using WP-CLI, this cheat sheet will be very useful. It has all official commands together with examples and global parameters.
It is possible to install WordPress through WP-CLI. However, you will need to create a MySQL database for your blog before proceeding. When using Hostinger, you can create the MySQL database in Control Panel -> MySQL Databases section.
If you are using WP-CLI on a VPS or a local system, follow these steps in order to make a new MySQL database. Just make sure that you have already installed MySQL on your Ubuntu or CentOS machine.
mysql -u root -p
Then, we can add a user and database by entering these lines one by one:
CREATE USER username; CREATE DATABASE databasename;
Don’t forget to change the values to your liking.
GRANT ALL PRIVILEGES ON databasename.* TO ‘username’ IDENTIFIED BY ‘yourpassword’;
quit
Now you can continue with the WordPress installation. However, you need to be in the public_html directory since it is usually where your website files should reside. To see the current directory, enter:
pwd
In case you are not in public_html, you have to move accordingly:
cd /home/username/public_html
wp core download

wp core config --dbname=wordpress --dbuser=user --dbpass=password --dbhost=localhost --dbprefix=wp_
Replace the default values with your own database details:
wp core install --url=yourdomain.com --title=Site_Title --admiwp corn_user=admin_username --admin_password=admin_password --admin_email=your@email.com
The output should be:
Success: WordPress installed successfully.
Good job! You have completed WordPress installation through WP-CLI.
An important thing to note, if you want to access and manage your WordPress blog using the command-line interface, you have to navigate to the public_html folder first.
cd /home/username/public_html
Let’s try to install a theme by using WP-XLI.
wp theme list
Output:
+-----------------+----------+--------+---------+ | name | status | update | version | +-----------------+----------+--------+---------+ | twentynineteen | active | none | 1.4 | | twentyseventeen | inactive | none | 2.2 | | twentysixteen | inactive | none | 2.0 | +-----------------+----------+--------+---------+
wp theme activate twentyseventeen
The output should be:
Success: Switched to 'Twenty Seventeen' theme.
wp theme search bootstrap
Output:
Success: Showing 10 of 292 themes.
+----------------------+----------------------+--------+ | name | slug | rating | +----------------------+----------------------+--------+ | Bootstrap Lightpress | bootstrap-lightpress | 0 | | Bootstrap News | bootstrap-news | 0 | | Bootstrap Photo | bootstrap-photo | 0 | | Bootstrap Beauty | bootstrap-beauty | 100 | | Bootstrap Journal | bootstrap-journal | 0 | | Bootstrap Blog | bootstrap-blog | 40 | | devdmbootstrap4 | devdmbootstrap4 | 100 | | WP Bootstrap 4 | wp-bootstrap-4 | 84 | | BPT Bootstrap | bpt-bootstrap | 74 | | BootstrapFast | bootstrapfast | 0 | +----------------------+----------------------+--------+
wp theme install WP-Bootstrap-4 --activate
To top it all off, you can also install a WordPress theme by specifying the directory of the zip archive or by entering the theme’s URL.
Next, we’ll take a look at plugin management using WP-CLI.
wp plugins list
Output:
+---------+----------+--------+---------+ | name | status | update | version | +---------+----------+--------+---------+ | akismet | inactive | none | 4.1.2 | | hello | inactive | none | 1.7.2 | +---------+----------+--------+---------+
wp plugin search "contact form 7"
Output:
Success: Showing 10 of 2514 plugins. +--------------------------------------+---------------------------+--------+ | name | slug | rating | +--------------------------------------+---------------------------+--------+ | Contact Form 7 | contact-form-7 | 92 | | Contact Bank - Contact Forms Builder | contact-bank | 86 | | Contact Form | contact-forms-builder | 90 | | Contact Form | contact-form-ready | 96 | | Contact Form | contact-form-add | 76 | | Contact Form | powr-contact-form | 100 | | Contact Form | better-contact-form | 100 | | Contact Form | contact-form-master | 96 | | Contact Form | contact-form-maker | 88 | | Contact Form by Supsystic | contact-form-by-supsystic | 92 | +--------------------------------------+---------------------------+--------+
wp plugin install contact-form-7 --activate
Same as with themes, plugins can be installed from .zip archive and a URL.
wp plugin delete contact-form-7
Output:
Deleted 'contact-form-7' plugin. Success: Deleted 1 of 1 plugins.
Updating WordPress with WP-CLI is a two-step process, as you need to update WordPress files and the database in order to complete this process.
wp core update
wp core update-db
wp theme update --all
wp plugin update --all
In this section, you will find how to use WP-CLI to manage your post and media.
WP-CLI provides several ways to manage content through the command line. It may not be very comfortable to write posts in the terminal. However, for the sake of learning, let’s see how to do it.
wp post list
Output:
+----+--------------+-------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+-------------+---------------------+-------------+ | 1 | Hello world! | hello-world | 2020-03-06 12:22:55 | publish | +----+--------------+-------------+---------------------+-------------+
wp post delete 1
Output:
Success: Trashed post 1.
wp post create --post_status=publish --post_title="This Post Was Created With WP-CLI" --edit
This command will open the vim text editor. Input the content and exit vim by pressing the ESC button, type :wq and press Enter.
You may check the new post on your blog.

wp post create ./post.txt --post_title='Sample Post' --post_status=publish
wp post generate --count=10
With WP-CLI, you can automate the image import process. For this example, we want to import all images from the images_for_site folder. We can achieve it by using a single command:
wp media import images_for_site/*
Thanks to WP-CLI, you can easily export or import content from one WordPress installation to another.
wp export
wp plugin install wordpress-importer --activate
wp import hostingertutorials.wordpress.2020-03-06.000.xml --authors=create
Output:
<p>All done. <a href="http://hostinger-dev-17.xyz/wp-admin/">Have fun!</a></p><p>Remember to update the passwords and roles of imported users.</p> Success: Finished importing from 'hostingerdev.wordpress.2020-03-06.000.xml' file.
WP-CLI also allows you to manage your database. Here are some basics:
wp db query "SELECT user_login,ID FROM wp_users;"
Output:
+------------+----+ | user_login | ID | +------------+----+ | user | 1 | +------------+----+
wp db export
Output:
Success: Exported to 'u373726772_wordpress-2020-03-06-3c44b24.sql'.
wp db import filename.sql
Output:
Success: Imported from 'filename.sql'.
Have you ever moved WordPress from a local development or staging to hosting? If you have, you know how useful search and replace can be. While an SQL query or a plugin might do it, you can easily perform search and replace in WP-CLI with one command.
Let’s say we have changed our domain name from hostinger-dev-17.xyz to hostinger-dev-17.net and we need to update all URLs in our WordPress database.
wp search-replace --dry-run 'hostinger-dev-17.xyz' 'hostinger-dev-17.net'
Output:
Success: 14 replacements to be made.
wp search-replace 'hostinger-dev-17.xyz' 'hostinger-dev-17.net'
Output:
Success: Made 14 replacements.
WP-CLI makes it possible for you to manage your WordPress site through a command-line interface. As you have seen, it can prove to be more efficient than performing tasks from your WordPress admin page.
What’s great, this tool is extendable through third-party programs and advanced commands. All in all, it’s a great tool to streamline your workflow and improve your productivity.
In this tutorial, we have shown you the basic foundation of WP-CLI. Hopefully, now you can master several tasks using the command line, such as installing and managing WordPress, plugins, themes, posts, databases, and so on.
Good luck and be sure to comment below if you have any questions!
Leave a reply