WordPress Tutorials

How to Use WP-CLI for Better WordPress Site Management

The command line interface has always been popular in the world of developers, as it provides much more productivity and speed when developing a site.

WP-CLI helps web developers overcome the most head-scratching tasks in developing a WordPress website. In this article, we’ll walk you through a tutorial on how to setup WP-CLI and use its different commands to develop even faster and more efficient website.

Let’s get right to it!

Some Preliminary Steps

Before we move on to the tutorial, we need to make sure some things are in place for the command line interface for advanced WordPress management to install successfully.

Access to SSH

First of all, you need to make sure you have Secure Shell (SSH) access to your hosting service as most hosting services don’t provide SSH access by default. If you don’t have access, contact your hosting service’s customer support center for assistance.

Updated Software

After you’ve crossed SSH access off your list, you want to make sure you have WordPress 3.4 (or above) activated, PHP 5.3.2 (or above), and a UNIX-like environment. If you are a Windows user, you will need a command-line tool like Cygwin or a virtual machine setup. Once you have all the required resources to run WP-CLI, you can proceed to install it on your WordPress website.

Installing WP-CLI

Installing WP-CLI is quite simple. All you have to do is download a PHP file and put it somewhere on your drive so that you can run it from anywhere – which should be your main objective while setting up the command line interface. First, you’ll need to download the WP-CLI script from the GitHub repository to get the latest version available.

Once you’ve downloaded the .phar file, you need to make it an executable file. To do that, run the following command:

chmod +x wp-cli.phar

The last step for installation is that you need to move the newly created executable file to a folder so that we can run it from anywhere. Another handy dandy trick is to rename the executable file to something that’s easy to remember. That’s why the most commonly used name is wp. Once you’ve renamed the file, place it in the wp folder on your drive by using the following command:

sudo mv wp-cli.phar /usr/local/bin/wp

Congratulations! You have successfully installed WP-CLI and you can now run WP-CLI commands. Just to make sure that everything is in place you can run the following command:

wp –info

If everything went fine, you should be able to see this information displayed on the command line interface afterward:

PHP binary: /usr/bin/php
PHP version: 5.5.24
php.ini used: /etc/php.ini
WP-CLI root dir:  phar://wp-cli.phar
WP-CLI global config: /Users/kouratoras/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 0.19.2

Now that we’ve successfully installed WP-CLI, let’s take a look at some of the commands you can run on it and the tasks they’ll accomplish.

Basic WP-CLI Commands and Tasks

Let’s start with the basics. You can easily install WordPress using WP-CLI’s core command. By using this command, you can make use of a set of powerful tools for managing WordPress installations.

Installing WordPress

First, we need to download WordPress in order to install it.  Navigate to the directory you’d like to download the WordPress CMS to and run the following command:

wp core download

This command will download the latest version of WordPress and after the download is complete, you can create the wp-config.php file using the core config command as follows:

wp core config --dbname=databasename --dbuser=databaseuser --dbpass=databasepassword --dbhost=localhost --dbprefix=prfx_

This command displays all of your login credentials which are usually present in the wp-config.php file. Now that the wp-config.php file is created, we can move on to installing WordPress. To install WordPress, you will have to execute this core install command:

[/php]wp core install –url=example.com  –title=”WordPress Website Title” –admin_user=admin_user –admin_password=admin_password –admin_email=admin@example.com[/php]

Check your credentials and make sure everything is in place. That’s all there is to it! You’ve installed WordPress on your server using WP-CLI.

Updating WordPress

Installing a clean, fresh copy of WordPress is one thing but updating WordPress is another. And just like installing WordPress, updating it is also very simple using WP-CLI. Using sub-commands, we are going to update our WordPress. The WP-CLI sub-commands enable users to update core files and database files if they aren’t updated. Here’s how:

wp core update
wp core update-db

But if you are not sure about your current version of WordPress then you can check it by using this command:

wp core version

Updating WordPress using the dashboard might seem much more efficient than it is through the command line but f you have several WordPress sites it can save you a lot of time and effort. By using WP-CLI, you can update multiple WordPress websites easily using this command:

#!/bin/bash
declare -a sites=('/var/www/wp1' '/var/www/wp2' '/var/www/wp3')
for site in "${sites[@]}";
do
wp --path=$site core update
done

Before any updating process, it’s always a good idea to backup your database.

Backup Database

WP-CLI has a one-liner WordPress backup command, the db export command:

wp db export backup.sql

It’s as simple as that! By running this command, you’ll have backed up your site’s database files in a jiffy.

Installing Themes

Installing themes with WP-CLI is very simple and less time consuming than going to the WordPress admin panel. WP-CLI connects your server directly to the WordPress theme repository and imports themes in just a few seconds. You can install any theme by using this command:

wp theme install themename

Just replace themename with the name of the theme you want to install.

Activating Themes

After installing the theme, you can activate it through WP-CLI using the following command:

wp theme activate themename

Activating themes through WP-CLI is easy to do and you’ll see the changes take effects in seconds.

Updating Themes

As you can probably already guess, updating WordPress themes with WP-CLI is just as easy as installing and activating them. The update command will save you a bunch of clicks and remembering how you’re supposed to update themes in the first place:

wp theme update themename

And if you want to update all of your themes while you’re at it, you can use this command instead:

wp theme update –all

This will help to update all of your themes in just a few seconds, unlike the traditional way of going to the admin panel and updating each theme individually.

Installing Plugins

Installing plugins with WP-CLI is an effortless task, as well. WP-CLI installs all of the plugins from the official WordPress plugin repository. You can use this command to install plugins:

wp plugin install pluginname

Simply replace pluginname with the name of the plugin you’d like to install to your site.

Activating Plugins

To activate the plugin, use the command:

wp plugin activate pluginname

That’s all there is to it! You’ve just installed a plugin to your WordPress using WP-CLI.

Deactivating Plugins

When you want to deactivate the plugin simply run the following command:

wp plugin deactivate pluginname

Updating Plugins

Similarly, updating plugins is a breeze. Whether you want to update a single plugin or all of the plugins you have installed, there’s a command for it with WP-CLI.

wp plugin update pluginname

Or if you’d like to update all of your plugins, just use this simple command:

wp plugin update –all

This command helps in updating all of your present plugins in seconds.

Managing Post Revisions

WordPress stores every revision of a post in your database which means that your site’s database contains a ton of information that you won’t ever have to use. Now that’s a really big issue. Unfortunately, WP-CLI does not manage post revisions with a simple command. That said, the wp-revisions-cli plugin, which is an extension of WP-CLI, helps in managing post revisions. This useful plugin can be found on the GitHub repository and you can install it just as you would install an ordinary WordPress plugin.

Bulk Import Images

Other management issues when administrating a WordPress site usually occur when handling media or images. Using WP-CLI, you won’t have to worry about a thing when it comes to media. The media tool of WP-CLI enables users to import all of their images and media onto their site with the following command:

wp media import images_folder/*

This is much easier compared to the ordinary admin panel method.

Search and Replace Functionality

A common practice employed by most developers is that they build a site on a local or development server and after completing the site, they move it to another server. In this process, copying and moving the database files is simple and easy to do. The tricky part is when you have to replace the old URLs with the new ones in the database. This tricky part won’t be so tricky if you are using WP-CLI. Using the search and replace command you can easily replace URLs in the database entries.

wp search-replace 'www.old-site.com' 'www.new-site.com'

This search and replace command helps developers finish their site in less time than expected. A lot of the hard work is done by WP-CLI so you won’t have to worry about making tiny mistakes or forgetting to update the URL for a particular page.

Wrapping It Up

WP-CLI is a must-have for all WordPress developers who want to speed up the development process without compromising on accuracy. There are endless possibilities with WP-CLI and it’s wide-range of features provide a good result to its users.

Have you used WP-CLI? We’d love to hear about your experience with it so let us know in the comments section below!

Rafay

View Comments

Recent Posts

SeedProd Review: A ‘New-Old’ Plugin to Help You Build Full WordPress Websites

SeedProd is one of the elders of the WordPress ecosystem. It's also a bit of…

6 days ago

Dealing with WordPress RSS Feed Errors: How to Identify and Fix Them

Really Simple Syndication (RSS) feeds are a vital component of the WordPress ecosystem. They act…

2 weeks ago

Top Black Friday and Cyber Monday Deals for 2024

It’s that time of year again when people start revving up their shopping lists, hunting…

3 weeks ago

6 Best WordPress Translation Plugins for a Multilingual Site (2025)

If you clicked on this post, I’m guessing you want to find the best solution…

2 months ago

How to Start an Online Course Using WordPress and LearnDash

Trying to figure out how to start an online course so that you can share…

2 months ago

Teachable Review (2025): Is It the Best Way to Create an Online Course?

Considering using Teachable to create an online course? In our hands-on Teachable review, we'll help…

3 months ago