Online Support

Never Heard of WP-CLI? Here’s a Quick Introduction

Never Heard of WP-CLI? Here’s a Quick Introduction

The WordPress command-line interface, WP-CLI, is hands-down one of the most useful tools ever created to efficiently manage WordPress websites. Learn to use it effectively and you’ll simplify complex WordPress tasks that would otherwise take a lot of time.

Right off the bat, I’d like to make it clear that WP-CLI is best used on a cloud server or a dedicated server with Linux installed. I’ve tried it on shared hosting, and while functional, it can be a bit more challenging to use due to technical limitations typically associated with shared hosting accounts.

So if you’re already on your own Linux server, read on.

What is WP-CLI?

To define WP-CLI, it’s worth looking at a normal command-line interface.

The command-line interface is a text-based user interface that allows you to do virtually anything you can do with a graphical user interface (GUI), and more. Easily run programs, manage your entire system, and generally interact with your system.

Here’s a screenshot of the CLI on my desktop:

Screenshot of a Linux command line interface

From the command prompt, I can enter a wide variety of commands which would produce the same result as if I had clicked somewhere on my system’s desktop. Here are a few examples:

  • By typing ls I can get a list of the contents of the folder I am currently in. This would be the same as opening up the same folder using my desktop’s file manager
  • I can copy a file from one location to another with the cp command
  • I can move a file from one location to another with the mv command
  • I can view real-time application resource usage using the top command

While all this typing might seem tedious compared to using a mouse to achieve the same, the command-line interface really comes into its own when it comes to more complex variations of those simple tasks.

For example, let’s say I have 10,000 different files in one folder, and I want to move all the images from my camera (JPGs) to a different location. With a graphical user interface, I’d have to sort the files by type, then copy / cut / move those files to their new location. Alternatively, I’d have to CTRL+click to select only those files I want to move – a tedious process.

With the CLI, the following command will do the trick:

mv *.jpg destination-folder/ (move all files ending with jpg to a folder called destination-folder)

And that’s just the tip of the proverbial iceberg. The CLI is capable of incredible complexity that would otherwise take huge amounts of time, or many different applications to complete.

The same is true for WP-CLI. Although you can do pretty much everything you can by clicking around in the WordPress dashboard, it really shines when it comes to complex tasks that would otherwise require a fair amount of technical skill or a multitude of different plugins.

Example WP-CLI commands

The commands below are intended to showcase some of the flexibility of WP-CLI, and some of the dashboard functions that can be performed without ever opening a web browser.

Manage your WordPress website

The number of commands available to manage your WordPress website is numerous. For virtually every function in the dashboard, there’s a WP-CLI command, and then some.

Let’s take a quick look at a few useful commands:

Plugins

List all plugins and their status

wp plugin status

Here’s an example of the output:

screenshot of WP CLI showing installed plugins

You’ll note that some plugins have a ‘U’, ‘A’, ‘I’, or ‘N’ to the left of the plugin name. Here’s what they mean:

N = Network Active, I = Inactive, A = Active, U = Update Available

WP-CLI can be used to manage plugins:

Search for plugin

wp plugin search ‘hello’

This command searches the WordPress plugin repository for plugins containing the word “hello”.

Install & Activate plugin

wp plugin install hello-dolly --activate

This command installs and activates the Hello Dolly plugin.

Update plugin

wp plugin update hello-dolly

This command updates the Hello Dolly plugin.

Deactivate plugin

wp plugin deactivate hello-dolly

This command deactivates the Hello Dolly plugin.

Delete plugin

wp plugin delete hello-dolly

This command deletes the Hello Dolly plugin.

Learn more about the wp plugin command

Themes

As with plugins, WP-CLI can also be used to manage themes. Here’s the command to show all themes:

List all themes

wp theme status

Here’s the output:

Screenshot of the WP-CLI showing a list of themes.

The plugin management commands used above can also be used for themes by swapping ‘plugin’ for ‘theme’.

Learn more about the wp theme command

Create a child theme

One example of a WP-CLI function that can’t be achieved through the dashboard is the creation of a child theme. In the example above you’ll see I have hello-elementor as the active theme. There is a publicly available child theme available on GitHub, but what if you could create one with just a simple command?

wp scaffold child-theme hello-child --parent_theme=hello-elementor --theme_name='Hello Child' --author='Leo' --author_uri=https://www.storminternet.co.uk --theme_uri=https://www.storminternet.co.uk --activate

The above command creates a child theme called Hello Child in a folder called hello-child. Of note here is that we used the ‘scaffold’ command, which can automatically generate code for themes, plugins, and more.

Learn more about the wp scaffold command

Security

WP-CLI provides some security features, too. While some security plugins already contain these features, they’re still pretty useful.

WordPress integrity check

wp core verify-checksums --version=$(wp core version)

This command verifies the integrity of your WordPress installation. It does this by downloading checksums for your current WordPress version, and comparing them against the currently installed files.

Learn more about the wp core command

Plugin integrity check

Given that plugin vulnerabilities are one of the most prominent ways attackers can get access to a WordPress website, it pays to have a command that can also verify the integrity of plugins. Keep in mind that this only works for plugins in the WordPress plugin repository.

wp plugin verify-checksums --all

Also keep in mind that since you can control virtually every dashboard feature using WP-CLI, you can greatly enhance the security of your site with a few lines of code that can remove admin panel menu items such as Users, Plugins, and Themes, and manage all from the command line.

Simplify complex tasks

Let’s assume you’re running a local WordPress site, and you want to move to a fresh install. The WP-CLI can be used to simplify complex tasks, like exporting selected tables:

Export from database

wp  db export users.sql --tables=wp_users,wp_usermeta

With this command, WP-CLI exports the wp_users and wp_usermeta tables to a file called users.sql. While the same can be achieved with the mysqldump command, WP-CLI automatically retrieves the database credentials from wp-config.php, saving you a few minutes.

Importing the tables is just as easy as exporting them, and can be achieved with the following command:

Import into database

wp  db import users.sql

Learn more about the wp db command

One of WP-CLI’s most useful features is a search and replace function. If you’ve ever tried to move a WordPress website from one domain to another, or needed to replace ‘http’ with ‘https’, you’ll know how useful such a function can be. Here’s the basic format of the command:

Search and replace

wp search-replace 'old' 'new'

Used in this format, WP-CLI will search through the entire WordPress database and replace ‘old’ with ‘new’.

It’s a powerful command, which means you’ll have to take the necessary precautions to avoid potentially disastrous mistakes. As such, we strongly recommend using a staging or test site. Then, add the –dry-run flag to determine which changes are made without making those changes permanent.

Search and replace – dry run

wp search-replace 'old' 'new' –dry-run

Learn more about the wp search-replace command

Where tasks similar to the above have to be performed on multiple WordPress sites, WP-CLI can be automated with simple shell scripts, ultimately saving a lot more time and cutting down the effort.

Wrapping up

The WordPress command-line interface is a very useful tool that can greatly simplify complex tasks. Importing and exporting databases or just a few tables, or performing a search and replace on database tables, can all be done with a single command. The same is true for managing themes, plugins, and checking that none of your WordPress files have been tampered with. Importantly, it minimises the need for additional plugins which can affect the performance and, potentially, the security of your website.

Are you a Storm Internet customer with a cloud server or a dedicated server? Get in touch with your Support Pod to get WP-CLI installed.

0800 817 4727