WordPress Tutorial

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

Introduction

WP-CLI is a tool that gives you the ability to administrate your WordPress through a command line interface. You can execute standard functions, such as plugin installations, as well as commands that are not supported through the standard WordPress back-end.

The biggest benefit to WP-CLI is the amount of time it saves you when you’re installing, configuring or maintaining WordPress based websites. If you have multiple sites, you no longer need to log in to each one to take care of basic tasks and the WP-CLI commands can also be automated.

In this WordPress tutorial you will learn how install and use WP CLI to its full potential.

What you’ll need

Before you begin this guide, you’ll need the following:

  • Access to the command line interface

Step 1 – Installing WP-CLI

On Hostinger shared hosting WP-CLI is already installed. Thus, you only need to connect to your account via SSH in order to use it.

If you want to install WP-CLI on VPS or a local Linux system, follow the steps below:

  1. Download the WP-CLI tool from Github with the following command:
  2. Confirm that it has downloaded successfully with this command:
  3. Now you need to set wp-cli.phar to be an executable file. Type:
  4. To finalize the installation, move WP-CLI into its final location:

Now that WP-CLI is installed, you can start working with your WordPress content and files through the command line.

Step 2 – Understanding WP-CLI Commands

WP-CLI is a straightforward tool if you’re already familiar with a command line environment. You gain the ability to control your blog through commands, which is often faster than going into the WordPress administration panel and clicking through various options. You also have the opportunity to script commands for streamlined processes.

WP-CLI comes with quite a few inbuilt commands. And the list of available WP-CLI commands can be extended by installing plugins which support WP-CLI. You can see the list of WP-CLI plugins along with the commands here. Let’s take a look at a few basic WP-CLI commands:

The command below shows the WP-CLI version information:

Example output:

The command needed in order to access the list of WP-CLI commands and help documentation:

Example output:

To exit WP-CLI help page, press Q button on your keyword.

You can also see a separate help page for each WP-CLI command. For example, if you want to know more about comment command, type in:

Example output:

As you can see from the example output, each WP-CLI command has a list of subcommands. For instance, to get the number of comments we can use the following subcommand:

And this is not the end, you can go down even further and see the help page for subcommand of a subcommand like this:

Example output:

WP-CLI Commands Cheat Sheet

If it’s the first time you are using WP-CLI, this cheat sheet will be useful for you. It has all official WP-CLI commands along with examples and WP-CLI global parameters.

WP-CLI Commands Cheat Sheet

Step 3 – Using WP-CLI to Install WordPress

You can handle the complete WordPress installation process through WP-CLI. However, you do need to create a MySQL database for your blog before proceeding. At Hostinger, MySQL database can be created in Control Panel -> MySQL Databases section.

If you are using WP-CLI on VPS or a local system, follow these steps in order to create a new MySQL database:

  1. Log in to MySQL:
  2. Make a database for your WordPress installation:
  3. Set up the username and password for the database:
  4. Download the WordPress installation files:

Once MySQL database is ready, you can proceed with WordPress installation. First of all, you need to download the newest WordPress version. You can do this by executing:

Example output:

IMPORTANT: make sure that you are in the correct directory before executing this command. Usually, all your website files should reside in public_html. You can see the directory in which you are in by executing the pwd command.

Once the download is complete, visit your website. You will see a standard WordPress installation form. Of course, you can go ahead and fill all required values by using a web browser. But since we have WP-CLI installed, it would be easier to do this in command line.

WordPress Installation Form

We will use the wp core command in order to add the MySQL database credentials to WordPress:

  • dbname – Your MySQL database name.
  • dbuser – Your MySQL database username.
  • dbpass – Your MySQL user password.
  • dbhost – Your MySQL server hostname.
  • dbprefix – MySQL database table prefix. You can leave it wp_.As you can see the MySQL credentials can be passed using wp core config parameters. If you entered MySQL details correctly, WP-CLI will generate wp-config.php file:

Example output:

Now, if you will visit your WordPress site again, you will notice that it asks to enter a few additional details.

WordPress Installation Form Enter Site Details

We will use the same wp core command in order to add these details and complete the WordPress installation:

Example output:

WordPress Installed with WP-CLI

Step 4 – Using WP-CLI to Install and Manage WordPress Themes

In this section, you will learn how to use WP-CLI in order to install WordPress themes. Let’s start by listing the already installed themes:

Example output:

From the status, we can see that the current active theme is twentyseventeen. Let’s say we wan to activate twentyfifteen:

Example output:

You can even use WP-CLI to search for new themes in the official WordPress themes directory. Let’s search for a theme with bootstrap support:

Example output:

You cannot say much about a theme from its name only, but ultrabootstrap sounds great and has a rating of 100. In order to install and activate the theme use:

Example output:

This is how our WordPress looks with the new theme activated:

WordPress New Theme Activated with WP-CLI

You can also specify the path of a WordPress theme zip archive if you have it on your hosting account. This command also works with URLs.

Step 5 – Using WP-CLI to Install and Manage WordPress Plugins

Just like themes, plugins can be installed and managed by using WP-CLI. In order to list the already installed plugins use:

Example output:

As you can see 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:

Example output:

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:

Example output:

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

In order to delete a plugin change install to delete:

Example output:

Step 6 – 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.

Start by updating WordPress core files:

Example output:

Run the command below to check and if its necessary to update the database to the required version:

Example output:

To update WordPress themes and plugins with WP-CLI change core to theme or plugin. –all parameter is used to update all installed themes/plugins, but you can change it to a specific theme/plugin name if needed:

For themes:

For plugins:

Step 7 – Using WP-CLI to Manage Content

Posts

WP-CLI provides several ways to manage your content through the command line. Yes, it may not be very comfortable to write posts in terminal, but for the sake of learning let’s see how to create and manage content with WP-CLI.

To see the list of all posts use:

Example output:

As you can see we only have one post – Hello world! . The ID of this post is 1. We can delete it with:

Example output:

To create a new post, use:

This command will open vim text editor. Input the content and exit vim by pressing the ESC button, entering :wq and pressing ENTER.

WP-CLI Create Post

It’s also possible to create a new post and import post content directly from the .txt file:

Apart from creating posts, with WP-CLI you can simply generate them. This feature is useful if you want to test how your WordPress site will work with a lot of posts. To auto generate 50 posts use:

Media

With WP-CLI you can automate image import process. Let’s say we want to import all images from folder images_for_site. We can achieve this by using a single command:

Step 9 – Using WP-CLI To Export/Import WordPress

With WP-CLI you can easily export or import contents from one WordPress installation to another. The following command will export posts, terms, authors, comments, and attachments to an XML file:

Example output:

Let’s say we want to import the same XML file to another WordPress instance. First of all, we need to install wp-importer plugin:

Once the installation is complete, we can proceed with the import. We will create a new author by using the –authors parameter:

Example output:

Step 10 – Using WP-CLI to Manage Database

You can run normal SQL queries with wp db query command. For example, in order to see all registered WordPress users use:

Example output:

You can run other useful operations like export. It will generate an SQL dump file of your WordPress database:

Example output:

To import an SQL file into WordPress database, use:

Example output:

Step 11 – Using WP-CLI to Search and Replace

If you have ever moved WordPress from local development or staging server to regular hosting, you know how useful search and replace can be. Of course, it can be done by using an SQL query or a plugin, but with WP-CLI search and replace can be performed with a single command. Let’s say we have changed our domain name from hostigner-tutorials.com to hostinger-tutorials.net and we need to update all URLs in our WordPress database.

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:

Example output:

Now, in order to perform the actual search and replace with WP-CLI, remove the –dry-run parameter:

Example output:

Conclusion

Outside of these basic commands, WP-CLI can be extended through third-party and advanced commands. Working with WordPress is much more streamlined when you no longer need to click through each section in the administration panel. If you work with multiple WordPress sites, you will improve your productivity. This tutorial gives you the foundation you need in order to handle most routine WP-CLI tasks and functions.

Add Comment

Click here to post a comment

Join thousands of subscribers worldwide

and get awesome deals & the newest tutorials to your email

Please wait...

Thank you for sign up!

Start saving now!

All-in-one web hosting & domain name from

$2
15
/mo
More in WordPress Tutorial
How to Fix Fatal Error: Maximum Execution Time Exceeded Error in WordPress

Close