Skip to main content

Introduction to WP-CLI

Published ago
Updated ago
4 min read

One of the things I like the most about WordPress is definitely the huge community that has been generated over the years, very capable and so intelligent that the enormous success of the tool simply would not be possible without them. Not to mention, it is thanks to this huge community that many (if not all) of the great functionalities of WordPress were created.

One of them, and one of the ones I use most commonly, is WP-CLI.

Table of contents

What is WP-CLI?

In very simple terms, it is nothing more than a command line with which we can control our site, it is based on PHP functions that are executed depending on the command we use, accepting the parameters we specify. The simplest example I use would be something like this:

1wp plugin list
2

This, something easy to understand, returns a list with the plugins of the site, if we want to activate one of them, we can simply execute:

1wp plugin activate akismet
2

And that's it! It's that simple to activate a plugin on our site using WP-CLI, as you can see it saves us a lot of clicks.

Installation

WP-CLI is supported by default in WordPress, but that doesn't mean that anyone can use it just by having WordPress, the command line is a tool that must be installed on the server where our WordPress installation is located, fortunately, it is very easy to install. For this article, we will assume that it is a local installation, so we will need to install the tool on our computer.

Note

Many local environments (like wp-env). Already come with the tool installed and ready to use, make sure to read the documentation of your system to know if it already comes included, and how to use it.

To install the tool, it is simply necessary to download and run a file, the official site of wp-cli has the instructions, which I copy below:

1# Download the file
2curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
3
4# Make sure it works
5php wp-cli.phar --info
6
7# Move it to our path
8chmod +x wp-cli.phar
9sudo mv wp-cli.phar /usr/local/bin/wp
10

Now we just need to run wp in our terminal and we will have something similar to this:

1wp --info
2
3# Output
4
5OS: Linux
6Shell: /usr/bin/zsh
7PHP binary: /usr/bin/php8.1
8PHP version: 8.1.0
9php.ini used: /etc/php/8.1/cli/php.ini
10MySQL binary: /usr/bin/mysql
11MySQL version: mysql Ver 8.0.27-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))
12SQL modes:
13WP-CLI root dir: /home/wp-cli/
14WP-CLI vendor dir: /home/wp-cli/vendor
15WP_CLI phar path:
16WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
17WP-CLI global config:
18WP-CLI project config: /home/wp-cli/wp-cli.yml
19WP-CLI version: 2.10.0
20

The basics

Once we have the tool installed, we can start using it, as I mentioned earlier, it is very simple, we just need to run wp followed by the command we want to run, for example, if we want to see the list of themes installed on our site, we simply run:

1wp theme list
2

And if we want to activate a theme, we simply run:

1wp theme activate twentytwentyfour
2

And so on, the official WP-CLI documentation is very complete and has examples of all the commands we can use.

WP-CLI Cheat Sheet

Here is a list of commands that I use frequently, and that have helped me save a lot of time:

WP Core

CommandDescription
wp core downloadDownloads the latest version of WordPress
wp core updateUpdates WordPress to the latest version
wp core installInstalls the latest version of WordPress

WP Config

CommandDescription
wp config createCreates a WordPress configuration file
wp config getDisplays the current WordPress configuration
wp config setUpdates the WordPress configuration

WP Plugin

CommandDescription
wp plugin listDisplays the list of installed plugins
wp plugin activateActivates a plugin
wp plugin deactivateDeactivates a plugin
wp plugin installInstalls a plugin
wp plugin uninstallUninstalls a plugin

WP Theme

CommandDescription
wp theme listDisplays the list of installed themes
wp theme activateActivates a theme
wp theme installInstalls a theme
wp theme uninstallUninstalls a theme

Conclusion

WP-CLI is a tool that has saved me a lot of time, and that has allowed me to automate many tasks that I had to do manually before, if you are a WordPress developer, I recommend you give it a try, I'm sure you won't regret it.

Until next time!

Read more