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.

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:

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.

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:

Log in to MySQL:

mysql -u yourusername -p

Make a database for your WordPress installation:

create database wordpress;

Set up the username and password for the database:

grant all on wordpress.* to 'username' identified by 'yourpassword';

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:

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.

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

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:

Success: Generated 'wp-config.php' file.

Now, if you will visit 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:

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

wp core update-db

Example output:

Success: WordPress database already at latest db version 38590.

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:

wp theme update --all

For plugins:

wp plugin update --all

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.

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

wp db export

Example output:

Success: Exported to 'u694443746_dapyz-a0d3f2a.sql'.

To import an SQL file into WordPress database, use:

wp db import file.sql

Example output:

Success: Imported from 'file.sql'.

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:

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

wp search-replace 'hostinger-tutorials.com' 'hostinger-tutorials.net'

Example output:

Success: Made 1008 replacements.

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.