How to Install and Use WP-CLI to Manage a WordPress Blog
access_time
hourglass_empty
person_outline

How to Install and Use WP-CLI to Manage a WordPress Blog

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 uncover what WP-CLI is.

What is WP-CLI?

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.

Installing 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:

  1. Download the WP-CLI tool from Github with the following command:
    curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  2. Next, confirm that it has been downloaded successfully:
    php wp-cli.phar --info
  3. Now you need to set wp-cli.phar to be an executable file. Simply enter:
    chmod +x wp-cli.phar
  4. To finalize the installation, move everything into the final location:
    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.

Understanding WP-CLI Commands

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 inbuilt 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:

Checking WP-CLI Version

Simply type this command to check the WP-CLI version information:

wp --info

The output should look like this:

PHP binary: /opt/alt/php70/usr/bin/php
PHP version: 7.0.33
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: /home/u499474900
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.2.0

Accessing The List of WP-CLI Commands and Help Documentation

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               Manage the object cache.
  cap                 Manage user capabilities.
  cli                 Manage WP-CLI itself.
  comment             Manage comments.
  core                Download, install, update and manage a WordPress install.
  cron                Manage 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.

Learning More About a Specific Command

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.
.....

WP-CLI Commands Cheat Sheet

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.

WP-CLI cheat sheet that contain important commands

Using WP-CLI to Install WordPress

It is possible to install WordPress through WP-CLI. However, you 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.

  1. First, we will log in to MySQL as root to create a new user.
    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.

  2. After that, we have to grant all permission to the user to modify the database.
    GRANT ALL PRIVILEGES ON databasename.* TO ‘username’ IDENTIFIED BY ‘yourpassword’;
  3. Once it’s done, log out from MySQL by typing:
    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
  1. If you are already on that directory, you can download the newest WordPress version by executing:
    wp core download
  2. Now that the download is complete, visit your website to check the installation. You will see a standard WordPress installation form. Although you can fill in all the required columns using a web browser, it would be easier to do that with WP-CLI.
    WordPress database detail form
  3. We will use the wp core command in order to add our MySQL database credentials to WordPress:
    wp core config --dbname=wordpress --dbuser=user --dbpass=password --dbhost=localhost --dbprefix=wp_

    Replace the default values with your own database details:

    • dbname – the MySQL database name.
    • dbuser – the database username.
    • dbpass – your MySQL user password.
    • dbhost – the MySQL server hostname.
    • dbprefix – MySQL database table prefix. You can leave it as wp_.
  4. Now if you go to your WordPress site again, you will notice that it asks to enter a few additional details.
    We will use the same wp core command in order to add these details and complete the WordPress installation:

    wp core install --url=yourdomain.com  --title=Site_Title --admin_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.

A newly installed WordPress site

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

Using WP-CLI to Install and Manage WordPress Themes

Let’s try to install a theme by using WP-XLI.

  1. Start by listing the already installed themes:
    wp theme list

    Output:

    +-----------------+----------+--------+---------+
    | name            | status   | update | version |
    +-----------------+----------+--------+---------+
    | twentynineteen  | active   | none   | 1.4     |
    | twentyseventeen | inactive | none   | 2.2     |
    | twentysixteen   | inactive | none   | 2.0     |
    +-----------------+----------+--------+---------+
  2. From the status, we can see that the active theme is twentynineteen. Let’s say we want to replace it with twentyseventeen.
    wp theme activate  twentyseventeen

    The output should be:

    Success: Switched to 'Twenty Seventeen' theme.
  3. You can even use WP-CLI to search for new themes in the official WordPress theme directory. In this example, we’d like to search for a theme with bootstrap support:
    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      |
    +----------------------+----------------------+--------+
  4. We know you cannot say much about a theme from its name only. That being said, Bootstrap Beauty sounds great and has a rating of 100. In order to install and activate the theme with WP-CLI, use:
    wp theme install bootstrap-beauty --activate

    This is how our WordPress site looks with the new theme activated.
    Bootstrap Beauty WordPress theme page
    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.

Using WP-CLI to Install and Manage WordPress Plugins

Next, we’ll take a look at plugin management using WP-CLI.

  1. Just like themes, you can install and manage plugins through the command line. In order to list the already installed plugins, enter:
    wp plugins list

    Output:

    +---------+----------+--------+---------+
    | name    | status   | update | version |
    +---------+----------+--------+---------+
    | akismet | inactive | none   | 4.1.2   |
    | hello   | inactive | none   | 1.7.2   |
    +---------+----------+--------+---------+
  2. As you might notice, we don’t have many plugins. Let’s install a contact form plugin for our WordPress blog. First, search the plugin directory for the contact form 7 plugin:
    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     |
    +--------------------------------------+---------------------------+--------+
  3. Contact Form 7 is the first one on the list. We want to install and activate it since it’s a great plugin for creating lightweight contact forms:
    wp plugin install contact-form-7 --activate

    Same as with themes, plugins can be installed from .zip archive and a URL.

  4. In order to delete a plugin change install to delete:
    wp plugin delete contact-form-7

    Output:

    Deleted 'contact-form-7' plugin.
    Success: Deleted 1 of 1 plugins.

Using WP-CLI to Update WordPress

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.

  1. Start by updating the WordPress core files:
    wp core update
  2. Run the command below to check and if it is necessary to update the database to a required version:
    wp core update-db
  3. This is how to update WordPress themes and plugins with the command line. Note that the –all parameter is used to update all installed themes/plugins, but you can change it to a specific theme/plugin by replacing –all with the item’s name.
    • For themes:
      wp theme update --all
    • For plugins:
      wp plugin update --all

Using WP-CLI to Manage Content

In this section, you will find how to use WP-CLI to manage your post and media.

Posts

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.

  1. To see the list of all posts use:
    wp post list

    Output:

    +----+--------------+-------------+---------------------+-------------+
    | ID | post_title   | post_name   | post_date           | post_status |
    +----+--------------+-------------+---------------------+-------------+
    | 1  | Hello world! | hello-world | 2019-10-09 12:22:55 | publish     |
    +----+--------------+-------------+---------------------+-------------+
  2. As you can see we only have one post (Hello world!) and the ID of this post is 1. We can delete it with:
    wp post delete 1

    Output:

    Success: Trashed post 1.
  3. To create a new post, use:
    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, typing :wq and pressing Enter.
    You may check the new post on your blog.
    A WordPress blog post created using WP-CLI

  4. It’s also possible to create a new post and import post content directly from a .txt file:
    wp post create ./post.txt --post_title='Sample Post' --post_status=publish
  5. Apart from creating posts, you can also generate them in WP-CLI. This feature is useful if you want to test how your WordPress site will work with a lot of posts. To autogenerate 10 posts, enter this following line:
    wp post generate --count=10

Media

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/*

Using WP-CLI To Export/Import WordPress

Thanks to WP-CLI, you can easily export or import content from one WordPress installation to another.

  1. In order to export posts, terms, authors, comments, and attachments to an XML file, enter the command below:
    wp export
  2. Let’s say we want to import the same XML file to another WordPress instance. First of all, we need to install the wp-importer plugin:
    wp plugin install wordpress-importer --activate
  3. Once the installation is complete, we can proceed with the import. We will create a new author by using the –authors parameter:
    wp import hostingertutorials.wordpress.2017-03-16.000.xml --authors=create

    Output:

    <p>All done. <a href="http://hostinger-dev-9.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.2019-10-09.000.xml' file.

Using WP-CLI to Manage Database

WP-CLI also allows you to manage your database. Here are some basics:

  1. The tool allows you to run normal SQL queries with the wp db query command. For instance, in order to see all registered WordPress users, enter:
    wp db query "SELECT user_login,ID FROM wp_users;"

    Output:

    +------------+----+
    | user_login | ID |
    +------------+----+
    | user       | 1  |
    +------------+----+
  2. You can run other useful operations like exports. It will generate an SQL dump file of your WordPress database:
    wp db export

    Output:

    Success: Exported to 'u499474900_wordpress-2019-10-09-3c44b24.sql'.
  3. To import an SQL file into a WordPress database, use:
    wp db import filename.sql

    Output:

    Success: Imported from 'filename.sql'.

Using WP-CLI to Search and Replace

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-9.xyz to hostinger-dev-9.net and we need to update all URLs in our WordPress database.

  1. It’s always recommended to make a ‘dry run’ before performing the actual search and replace. It will show how many instances would be changed:
    wp search-replace --dry-run 'hostinger-dev-9.xyz' 'hostinger-dev-9.net'

    Output:

    Success: 14 replacements to be made.
  2. Now, in order to perform the actual search and replace with WP-CLI, remove the –dry-run parameter:
    wp search-replace 'hostinger-dev-9.xyz' 'hostinger-dev-9.net'

    Output:

    Success: Made 14 replacements.

Conclusion

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!

The Author

Author

Domantas G. / @domantas

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.

Related tutorials

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in WordPress
How to Fix Fatal Error: max_execution_time Exceeded Error in WordPress

Close