5 Time-Saving Uses for WP-CLI Automation

WP-CLI Automation

What is WP-CLI?

The WP-CLI is a tool that allows you to perform actions on a WordPress installation straight from the command line. WP-CLI automation is the automating of repetitive manual tasks by using WP-CLI driven scripts. This may seem unnecessary, awkward, or too difficult a task to bother with when you’re deploying or managing a single WordPress installation, but when you’re managing many, or constantly creating similar new sites for testing, it becomes an extremely valuable tool to have in your developer toolkit.

About WP-CLI

With WP-CLI, you can essentially perform any action that you could have via the admin panel, but from the command line instead. You can install or update core WordPress files, plugins, or themes. You can activate and deactivate plugins or regenerate image thumbnails. You can also perform database actions, such as export and import of the database, or find and replace the database for information, such as a changed URL during a migration.

Some plugins have WP-CLI support as well — including many of the more popular ones. This means you can set up automated scripting to install and set up WordPress, install those plugins, and then to set up the plugins as well, using their own customized WP-CLI commands!

WP-CLI Automation

WP-CLI automation goes beyond simple command line usage when setting up or managing multiple WordPress installations. The ability to update or back up multiple sites at once, or create complicated boilerplate installations repeatedly with single commands are incredibly useful and can save a significant amount of time for maintainers of those sites.

If you don’t already have the WP-CLI installed, take a look at the installation documentation and get the WP-CLI up and running.

Five Use Cases for WP-CLI Automation

Installing WordPress

Once WordPress is installed, this example script could download, configure, and install WordPress core, remove starting plugins, add and activate a specified theme (SitePoint’s own Base Theme), and install and activate a list of plugins you’d prefer to use with new installations.


#!/usr/bin/env bash

#plugins to install and activate (slugs)
WPPLUGINS=( test-plugin1 test-plugin2 test-plugin3 )

echo "Starting WordPress Installation Script"

# Site Name Input
echo "Site Name: "
read -e sitename

# Site URL Input
echo "Site URL: "
read -e siteurl

# Download WP and configure it
wp core download
wp core config --dbname=$dbname --dbuser=root --dbpass=root
wp db create
wp core install --url=$siteurl --title="$sitename" --admin_user="admin" --admin_password="examplePassword123" --admin_email="test@example.com"

# Remove default plugins, install plugins, install Base Theme
wp plugin delete --all
wp theme install sitepoint-base.zip --activate
wp plugin install ${WPPLUGINS[@]} --activate

echo "WordPress installation complete!"

However, you could automate this process even further, by asking the user for relative path information, so that you don’t have to be in the installation directory to run it, by asking for database name and password, and more. You can also do (as you’ll see later in this article) a setup for a hosting environment that handles multiple WordPress installations on one server, and set up and install more than one site at once. Customize the script in the way that you need, so that it can be maximally effective for your own projects, and so that you won’t have to constantly rewrite it — make it efficient!

Continue reading %5 Time-Saving Uses for WP-CLI Automation%

Source: Sitepoint