Saturday, May 21st, 2022

WP-CLI: The First Steps to WordPress’s Command Line Tools

Before any trend of using composer, npm, yarn or any other command line tools within WordPress theme and plugins development, devs are already using a set of command line tools that are specifically designed for WP instances. were designed for management.

WP-CLI is a command line tool that is extensible, comprehensive, and easy to use. Even for tasks such as updating plugins, reinstalling WP core, or optimizing databases, it should be the first option for those developing properly under a WP-based environment. Huh.

Most of the popular VPN and cloud servers already have WP-CLI installed and running, but sometimes we need to install and setup it before using command line tools. If this is your case, simply access the SSH prompt in your server or cloud and proceed with the installation following a few steps:

# First, download WP-CLI package using Curl (or Wget)
curl -O  
# Secondly, we want to make wp-cli.phar executable from the prompt
chmod +x wp-cli.phar
# Finally, we move it to a user directory as wp command
sudo mv wp-cli.phar /usr/local/bin/wp

first contact

Well, if the installation took place without any errors or exceptions, it means that you can already use wp Order. From your SSH prompt, just type wp and enter. This will list all the common sub-commands available (as shown in the figure). There’s a lot we can do with WP-CLI, so let’s go over the main and most basic features in this article.

Basically, the initial purpose of WP-CLI is to provide a site administrator with the tools to manage every single aspect of a website, without the need for logging into the back-end. In addition, it has tools for optimizing and dealing with assets, users, and databases – meaning most plugins that do the same thing can be easily removed from your instance.

some basic commands

WP-CLI comes with a few commands and is extensible, which means that new commands and tools can be added. However, for this first contact, let’s get down to the basics. Let’s start with some routines with plugins. First, we will retrieve a list of all plugins, active or not, using the command line.

username@host [/var/www/html]# wp plugin list
| name           | status   | update    | version |
| akismet        | active   | available | 3.4     |
| google-captcha | active   | none      | 1.31    |
| hello          | inactive | none      | 1.6     |
| jetpack        | active   | none      | 5.3     |
| woocommerce    | active   | none      | 5.2     |

When getting the list, you can see that the Akismet plugin has an available update – so let’s update it using the command line instead of logging into the backend:

username@host [/var/www/html]# wp plugin update akismet
Enabling Maintenance mode...
Downloading update from
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
| name    | old_version | new_version | status  |
| akismet | 3.4         | 3.5         | Updated |
Success: Updated 1 of 1 plugins.

After this first contact, you should be familiar with the main WP-CLI commands before making any changes to your WP installation. To do this, all you need to do is type wp help And To enter. This will list all available commands for WP-CLI (new ones that may be added eventually will also appear).

This gives you a list of all commands, and if you want to get more information on one of those commands in particular, you just need to use wp help <command>.

username@host [/var/www/html]# wp help role

  Manages user roles, including creating new roles and resetting to defaults.


  wp role 


  create      Creates a new role.
  delete      Deletes an existing role.
  exists      Checks if a role exists.
  list        Lists all roles.
  reset       Resets any default role to default capabilities.

  See references for [Roles and Capabilities][1] and [WP User class][2].



    # List roles.
    $ wp role list --fields=role --format=csv

    # Check to see if a role exists.
    $ wp role exists editor
    Success: Role with ID 'editor' exists.

    # Create a new role.
    $ wp role create approver Approver
    Success: Role with key 'approver' created.

    # Delete an existing role.
    $ wp role delete approver
    Success: Role with key 'approver' deleted.

    # Reset existing roles to their default capabilities.
    $ wp role reset administrator author contributor
    Success: Reset 3/3 roles.


      Path to the WordPress files.

      Pretend request came from given URL. In multisite, this argument is how the target site is specified.

      Perform operation against a remote server over SSH (or a container using scheme of "docker", "docker-compose", "vagrant").

      Perform operation against a remote WordPress installation over HTTP.

      Set the WordPress user.

      Skip loading all plugins, or a comma-separated list of plugins. Note: mu-plugins are still loaded.

      Skip loading all themes, or a comma-separated list of themes.

      Skip loading all installed packages.

      Load PHP file before running the command (may be used more than once).

      Whether to colorize the output.

      Show all PHP errors and add verbosity to WP-CLI output. Built-in groups include: bootstrap, commandfactory, and help.

      Prompt the user to enter values for all command arguments, or a subset specified as comma-separated values.

      Suppress informational messages.


This is just the information and description for the “role” command. WP-CLI provides a comprehensive support for all available commands, which means you can learn while using it.

Why the struggle?

People who start coding in WP constantly struggle to create post types, taxonomies or even use some complicated and advanced plugin and theme boilerplate to develop their first module on WordPress. The fact is that the WP-CLI can be used not only to manage an installation, but also to create new types, scaffold plugins, create themes, code, and even create Gutenberg blocks.

Over time, the WP-CLI Simple Structures prove to be much easier and faster than the npm dependencies and built-in starters and boilerplates of using Composer. Of course you can get there, but if you’re not used to those tools, you better start with the basics – and that’s exactly what the WP-CLI tools can provide.

In the next post, we’ll show how to develop a simple plugin and Gutenberg’s Blocks, starting with the scaffolding structure of the WP-CLI. stay tuned!

Source link