Introduction to WP-CLI
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 list2
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 akismet2
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.
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 file2curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar34# Make sure it works5php wp-cli.phar --info67# Move it to our path8chmod +x wp-cli.phar9sudo mv wp-cli.phar /usr/local/bin/wp10
Now we just need to run wp
in our terminal and we will have something similar to this:
1wp --info23# Output45OS: Linux6Shell: /usr/bin/zsh7PHP binary: /usr/bin/php8.18PHP version: 8.1.09php.ini used: /etc/php/8.1/cli/php.ini10MySQL binary: /usr/bin/mysql11MySQL 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/vendor15WP_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.yml19WP-CLI version: 2.10.020
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 list2
And if we want to activate a theme, we simply run:
1wp theme activate twentytwentyfour2
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
Command | Description |
---|---|
wp core download | Downloads the latest version of WordPress |
wp core update | Updates WordPress to the latest version |
wp core install | Installs the latest version of WordPress |
WP Config
Command | Description |
---|---|
wp config create | Creates a WordPress configuration file |
wp config get | Displays the current WordPress configuration |
wp config set | Updates the WordPress configuration |
WP Plugin
Command | Description |
---|---|
wp plugin list | Displays the list of installed plugins |
wp plugin activate | Activates a plugin |
wp plugin deactivate | Deactivates a plugin |
wp plugin install | Installs a plugin |
wp plugin uninstall | Uninstalls a plugin |
WP Theme
Command | Description |
---|---|
wp theme list | Displays the list of installed themes |
wp theme activate | Activates a theme |
wp theme install | Installs a theme |
wp theme uninstall | Uninstalls 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!