How to Optimize Your Agency’s Workflow with WP-CLI

Is your agency spending a lot of time on busy work and repeating the same efforts time and time again, from one client to the next? Is the development team wasting a lot of their focus and mental energy on menial tasks and boilerplate work? What if you could free up some or all of these resources and invest them into the added value that the client will see and evaluate instead?

WP-CLI is a command-line tool that can help you accelerate the way you interact with WordPress websites. It is also a framework you can use to formalize and automate all of the processes that are shared amongst your clients if you’re a developer or an agency. Because of its versatility and simplicity of use, WP-CLI has been part of SiteGround’s preinstalled toolkit since 2013. SiteGround has also been one of the first sponsors of the project and continues to do so for the past 3 years, 2020 including. One of the main reasons for SiteGround’s support is the fact that WP-CLI perfectly complements their mission to provide powerful, yet simple to use tools for WordPress processes automation and optimization.

As one of the main co-maintainers of the WP-CLI ecosystem, I was really glad to accept SiteGround’s invitation to share some practical tips on how you can make use of WP-CLI to level up your workflows and have your clients get more bang for their buck!

Accelerating administrative efforts

WP-CLI is first and foremost a direct replacement to a WordPress site’s admin dashboard. Instead of providing a graphical web interface where you click through menus to get things done, it provides a text-driven command-line interface to perform these same tasks using simple written commands. What at first sounds like added effort eventually turns out to be an infinitely more expressive way of letting the site know what you need to have done, and this makes it scale so much better for complex use cases.

As a result, while it is not necessarily faster to type a command to make a change to a post than it is to click the corresponding button on that same post, the difference becomes more apparent once you deal with multiple posts instead. While you might shudder at the thought of manually making a change on the admin backend to thousands of posts on a large site, all you need for doing so with WP-CLI still happens to be a fairly simple command, usually a one-liner. Granted, it will take a bit more time to execute than it would for a single post, but you can just leave it running in the background and focus on something else in the meantime.

To show an example of this effect, let’s imagine you have a huge multisite network with thousands of sites. An existing user has proven their worth in terms of helping moderate the entire network and is to be promoted to being an administrator on all the sites. How would you go about doing this via the graphical admin backend?

It turns out that this is quite easy to do via WP-CLI:

wp site list --field=url | xargs -n1 -I {} wp --url={} user set-role <user-to-promote> administrator

The above command will retrieve the list of all site URLs for the network, and for each of these sites, set the role of the user in question to that of “administrator”. And while this might take a few minutes to complete, it is a one-liner that does the work for you. Using the web-based admin backend would probably cost you hours to do the same, or require you to write a one-off plugin to do so in a more efficient way.

For an overview of all the bundled commands that come by default with WP-CLI, you can browse the command reference at https://developer.wordpress.org/cli/commands/.

Using agency-wide defaults

WP-CLI supports configuration files at the global level as well as at the project level.

The global configuration file is best used for defining a developer’s personal preferences. The project-specific configuration file however should best be managed centrally across the agency and treated as a part of the project, just like your composer.json file.

To use a project-specific configuration file, all you need to do is create a wp-cli.yml file within the site’s root folder. It will accept a few entries that are specific to configuration files, like providing an array of commands to disable for that specific site. But it will also accept default values for any of the available commands with a unified syntax.

Let’s look at an example configuration file for a hypothetical project:

# WordPress Core is installed in a subfolder.
path: wp-core/
# Load WP-CLI-specific init code before executing a command.
require: wp-cli-init.php

# Provide default flags for the config create command.
config create:
    dbuser: root
    dbpass: 
    extra-php: |
        define( 'WP_DEBUG', true );
        define( 'WP_POST_REVISIONS', 50 );

# '_' is a special value denoting options for this wp-cli.yml.
_:
    # Inherit configuration from an arbitrary YAML file.
    inherit: agency-defaults.yml
    # Merge subcommand defaults instead of overriding.
    merge: true

As you can see, it is pretty straight-forward to provide defaults for any known command. Also, you can load centralized YAML files within such a project-specific config file as well, if you need it.

You can read more about WP-CLI configuration files at https://make.wordpress.org/cli/handbook/references/config/.

Automating recurring tasks

After you’ve used the command-line for a while to deal with administrative site work, you might start to notice recurring patterns. Are you always installing the same set of plugins to get started? Are you deleting a set of options from the database every time you want to test the onboarding flow? Maybe you constantly need to reset a user’s meta values to trigger that one piece of logic in your member’s area that is constantly being changed?

Instead of needing to remember a list of multiple commands and hopefully typing them without spelling mistakes, you should take a minute and put these into a shell script to automate that work. After all, a shell script is nothing more than a “step-by-step replay” of doing something manually in the console.

As an example, here’s a script that will automate the installation of a new WordPress site:

#!/usr/bin/env bash

# Configure the script to exit immediately if any command fails.
set -e

# Download WordPress core files.
wp core download

# Create wp-config.php file.
echo "Please enter your database credentials:"
wp config create --prompt

# Install WordPress.
echo "Please enter your WordPress installation details:"
wp core install --prompt

# Install standard plugins.
echo "Installing plugins..."
wp plugin install query-monitor user-switching wordpress-seo

# Activate and configure a few plugins:
echo "Configuring plugins..."
wp plugin activate wordpress-seo
wp option patch update wpseo_titles metadesc-home-wpseo "My new website"

echo "Done!"

You can, of course, improve this script over time to add more bells & whistles or to give more precise feedback. Sharing it with all of the agency’s developers makes sure you only need to invest once into the automation part, for everyone to reap the benefits later.

Furthermore, a collection of such scripts makes for excellent onboarding help when new developers join your team.

Extending for custom use cases

With more complex projects come more complex administrative requirements. WP-CLI provides its framework to developers so they can easily create their own custom commands to solve very specific business needs in an efficient way.

Running bulk checks across the entire range of an online shop’s products? No need to build an extra user interface for that – just wrap the checking logic in a WP-CLI command and you’re good to go. Then, take it a step further and automate these checks by running that command at a recurring schedule via a cron job!

Note that you can either include these custom commands within a site’s plugin or theme, or you can publish them as a separate package that can be installed via WP-CLI’s built-in package manager:

wp package install awesome-company/manage-all-the-things

While the range of commands bundled with WP-CLI already covers quite a few use cases, the possibility to build your own custom commands removes any remaining limits and leaves it up to your imagination only as to what you can do.

Executing tasks on remote sites

WP-CLI can connect to remote sites directly via the –ssh flag, provided that the WP-CLI binary is also installed and accessible on the remote machine:

wp --ssh=admin_user@123.456.78.90/var/www/my_site config set WP_DEBUG --raw true

What’s more, you can define aliases to denote the individual machines:

wp cli alias add @staging --set-ssh=123.456.78.90 --set-path/var/www/my_site --set-user=admin_user
wp @staging config set WP_DEBUG --raw true

The most powerful property of this is yet to come: you can group these aliases, and run a command on a group of machines instead of only a single machine. The built-in group @all is added by default, running the command on all the machines for which the alias was defined. But you can add your own groups that define a custom subset of machines. Groups can overlap, of course, and groups can contain other groups as well, letting you create an entire hierarchy of site management goodness! With these groups in place, you’ll do things like update all plugins on all staging sites, or add a user to all of your multisite networks, etc…

Browse to this link to learn more about connecting remotely to your servers or development machines.

Reaching for the black belt

To truly reap the biggest benefits from WP-CLI, you’ll want to combine the use of script automation, shared configuration, custom commands, and site aliases to ensure you cannot only address all of your agency’s usual needs but also do so at the exact point they are needed in one fell swoop.

Keep in mind that most of that work can be shared by and to all the members of the team. The return on the time you invest in this form of tooling will be multiplied by the members on your team that make use of them – it’s therefore very hard not to get a substantial benefit out of this!

In the end, optimizing the time it requires to deal with menial tasks and streamlining your workflows is what regains this time so you can use it where it matters most – creating value for your clients and gaining a competitive edge in a crowded market!

Getting started

The best place to get started right now is to read the WP-CLI documentation at https://make.wordpress.org/cli/handbook/. If you hit blocking issues or just have the odd question, head on over to the make.wordpress.org Slack team via https://make.wordpress.org/chat/ and join the #cli channel!

Alain Schlesser

WP-CLI Maintainer

WordPress

Start discussion

Ready to get your website started?

Choose a hosting plan, start or migrate your site in a few clicks, and grow your online presence!

Get Started Chat with an expert