WP-CLI: When WordPress and the Command Line Unite

If you’re a developer in the WordPress space and haven’t yet tapped into the power of WP-CLI, this is the article for you. In a nutshell, WP-CLI is a command line interface for WordPress, allowing you to do almost anything that can be done in the core WordPress admin by typing a few commands into your terminal of choice. Developers can also take advantage of WP-CLI’s extensibility and create their own commands, which we’ll cover this latter topic in a future post. For now let’s get started with the basics.

Table of Contents:

How to install WP-CLI:

WP-CLI can be installed through one of two methods: cURL or wget…

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Then verify that it works…

php wp-cli.phar --info

For a simpler way to use it from the command line, make the file executable and move it somewhere in your PATH. For example…

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

To find a directory currently in your path you can type the following…

echo $PATH

and you should see something like this…

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/damonsharp/.composer/vendor/bin

If this process completed successfully, type the following…

wp --info

and you should see something similar to the following…

OS:	Darwin 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64
Shell:	/bin/bash
PHP binary:	/usr/bin/php
PHP version:	7.1.7
php.ini used:	
WP-CLI root dir:	phar://wp-cli.phar
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/Users/damonsharp/Sites/codivated/codivated-dev
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	1.5.0

How to update WP-CLI:

WP-CLI can be updated to the latest version using the following command…

wp cli update

Common WP-CLI commands:

Now that WP-CLI is installed, let’s walk-through some use cases.

Getting Help

Whether you’re just getting started with WP-CLI or an everyday user, you’ll probably still need to get help with a parameter, or with a core command. Look no further than the following command…

wp help [command] [subcommand]

Here are a few examples…

wp help core
wp help core install
wp help search-replace
wp help scaffold _s

You get the idea. When in doubt, the help command is your friend.

WordPress Core Commands

Download WordPress core…

wp core download

Install core download above with a few specifics and no welcome email sent to the admin user…

wp core install --url=http://codivated.local> --title="Site Title" --admin_user=codivated --admin_password="$9Fls*3s;" [email protected] --skip-email

Check for core update…

wp core check-update

Sometimes it’s beneficial to check that the current WordPress core files match those from WordPress.org. For example this can be very helpful when taking over the development of an existing site, and you want me make sure core WordPress files are as they should be.

wp core verify-checksums --version=4.9.4 --locale=en_US

Plugins

Using WP-CLI can help with a variety of plugin actions, including listing, installing, activating, and deactivating, among other things.

We can easily list installed plugins, which will provide a nice table, including name, status, update, and version…

wp plugin list

+-------------------------------------------------+----------+-----------+---------+
| name                                            | status   | update    | version |
+-------------------------------------------------+----------+-----------+---------+
| caldera-forms                                   | inactive | none      | 1.5.9.1 |
| campaign-monitor-wp                             | inactive | none      | 2.1.0   |
| contact-form-7                                  | inactive | none      | 5.0.1   |
| debug-bar                                       | inactive | none      | 0.9     |
| debug-bar-actions-and-filters-addon             | inactive | none      | 1.5.4   |
| debug-bar-slow-actions                          | inactive | none      | 0.8.4   |
| debug-bar-transients                            | inactive | none      | 0.5     |
| easy-digital-downloads                          | inactive | none      | 2.9     |
| fh-custom-roles                                 | inactive | none      | 1.0.0   |
| gutenberg                                       | inactive | none      | 2.4.0   |
| loco-translate                                  | inactive | none      | 2.1.2   |
| mailchimp                                       | inactive | none      | 1.5.7   |
| members                                         | inactive | none      | 2.0.2   |
| ninja-forms                                     | inactive | none      | 3.2.16  |
| php-compatibility-checker                       | inactive | none      | 1.4.5   |
| query-monitor                                   | inactive | none      | 2.17.0  |
| rtl-tester                                      | inactive | none      | 1.1     |
| Simple-Calendar                                 | active   | none      | 3.1.15  |
| Simple-Calendar-FullCalendar                    | active   | none      | 1.0.4   |
| Simple-Calendar-Google-Calendar-Pro             | active   | none      | 1.0.5   |
| wp-storemapper                                  | inactive | none      | 1.0.0   |
| translation-exchange                            | inactive | none      | 1.0.14  |
| woocommerce                                     | inactive | available | 3.3.3   |
| woocommerce-gateway-paypal-powered-by-braintree | inactive | none      | 2.1.1   |
| woocommerce-services                            | inactive | none      | 1.12.1  |
| woocommerce-gateway-stripe                      | inactive | none      | 4.0.7   |
| wordpress-importer                              | inactive | none      | 0.6.4   |
| wpforms-lite                                    | inactive | available | 1.4.5   |
| wptouch                                         | inactive | none      | 4.3.27  |
| wptouch-pro                                     | inactive | none      | 4.3.24  |
| wp-user-avatar                                  | inactive | none      | 2.0.9   |
+-------------------------------------------------+----------+-----------+---------+

Get information about a specific plugin…

wp plugin get gutenberg

...
+-------------+---------------------------------------------------------------------------------------+
| Field       | Value                                                                                 |
+-------------+---------------------------------------------------------------------------------------+
| name        | gutenberg                                                                             |
| title       | Gutenberg                                                                             |
| author      | Gutenberg Team                                                                        |
| version     | 2.4.0                                                                                 |
| description | Printing since 1440. This is the development plugin for the new block
editor in core. |
| status      | inactive                                                                              |
+-------------+---------------------------------------------------------------------------------------+

Activate a plugin…

wp plugin activate gutenberg

Deactivate a plugin…

wp plugin deactivate Simple-Calendar

Check if a plugin installed from the wordpress.org/plugins repository has a verified checksum (i.e. it matches what came from the repository). This should result in a “Success: Verified 1 of 1 plugins”…

wp plugin verify-checksums gutenberg

If the plugin doesn’t exist in the repository this check will throw a warning and skip the check. If this verification fails then the plugin being tested doesn’t match what’s in the repository and could indicate that the plugin’s core files have been modified. This can help in troubleshooting cases of weird plugin behavior.

Search for a plugin in the wordpress.org repository…

wp plugin search form

would give you something like this…

+----------------------------------------------------------------------------+-----------------------------------+--------+
| name                                                                       | slug                              | rating |
+----------------------------------------------------------------------------+-----------------------------------+--------+
| Contact Form 7                                                             | contact-form-7                    | 90     |
| Contact Form by WPForms – Drag & Drop Form Builder for WordPress | wpforms-lite                      | 98     |
| Ninja Forms – The Easy and Powerful Forms Builder                    | ninja-forms                       | 88     |
| Form Maker by WD – user-friendly drag & drop Form Builder plugin | form-maker                        | 90     |
| Caldera Forms – More Than Contact Forms                              | caldera-forms                     | 90     |
| Popup Maker – Popup Forms, Optins & More                         | popup-maker                       | 98     |
| Calculated Fields Form                                                     | calculated-fields-form            | 94     |
| MailChimp for WordPress                                                    | mailchimp-for-wp                  | 98     |
| Formidable Forms – Form Builder for WordPress                        | formidable                        | 88     |
| Easy Forms for MailChimp                                                   | yikes-inc-easy-mailchimp-extender | 94     |
+----------------------------------------------------------------------------+-----------------------------------+--------+

Install a plugin from the wordpress.org repository…

wp plugin install ninja-forms

Uninstall a plugin…

wp plugin uninstall ninja-forms

Delete a plugin’s files without deactivating or uninstalling...

wp plugin delete ninja-forms

Themes

As with plugins, themes can be easily viewed, activated, deactivated, etc.

List themes…

wp theme list

...
+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| storefront      | inactive | none   | 2.2.8   |
| twentyeleven    | inactive | none   | 2.7     |
| twentyfifteen   | inactive | none   | 1.9     |
| twentyfourteen  | inactive | none   | 2.1     |
| twentyseventeen | inactive | none   | 1.4     |
| twentysixteen   | active   | none   | 1.4     |
+-----------------+----------+--------+---------+

Get information about a specific theme…

wp theme get twentyseventeen
+----------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Field          | Value                                                                                                                             |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------+
| name           | Twenty Seventeen                                                                                                                  |
| title          | Twenty Seventeen                                                                                                                  |
| version        | 1.4                                                                                                                               |
| parent_theme   |                                                                                                                                   |
| template_dir   | /Users/damonsharp/Sites/codivated/codivated-dev/wp-content/themes/twentyseventeen                                                 |
| stylesheet_dir | /Users/damonsharp/Sites/codivated/codivated-dev/wp-content/themes/twentyseventeen                                                 |
| template       | twentyseventeen                                                                                                                   |
| stylesheet     | twentyseventeen                                                                                                                   |
| screenshot     | screenshot.png                                                                                                                    |
| description    | Twenty Seventeen brings your site to life with header video and immersive featured images. With a focus on business sites, it fea |
|                | tures multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asym |
|                | metrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works |
|                |  great in many languages, for any abilities, and on any device.                                                                   |
| author         | the WordPress team                                                                           |
| tags           | ["one-column","two-columns","right-sidebar","flexible-header","accessibility-ready","custom-colors","custom-header","custom-menu" |
|                | ,"custom-logo","editor-style","featured-images","footer-widgets","post-formats","rtl-language-support","sticky-post","theme-optio |
|                | ns","threaded-comments","translation-ready"]                                                                                      |
| theme_root     | /Users/damonsharp/Sites/codivated/codivated-dev/wp-content/themes                                                                 |
| theme_root_uri | http://codivated.test/wp-content/themes                                                                                           |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------+

Activate a theme…

wp theme activate twentyeleven

Update a theme…

wp theme update twentyeleven

Scaffolding

Did you know that you can easily generate a child theme, scaffold an entire plugin including tests, gutenberg blocks, or even the starter “_s” theme, etc.? Here’s how…

Scaffold a plugin…

wp scaffold plugin some-plugin

Generate a child theme from an existing theme…

wp scaffold child-theme twentyseventeen-child --parent-theme="twentyseventeen" --theme-name="Twentyseventeen Child Theme" --author="Codivated" --author-uri="https://codivated.com"

Generate a Gutenberg/block editor block…

wp scaffold block my-block --title="My Block"  [--plugin="My Block Plugin"

Generate a theme based on the _s theme, including a stylesheet as Sass…

wp scaffold _s codivated_s --theme_name="Codivated _S Theme" --author="Codivated" --sassify

To see more options use the following command…

wp help scaffold

Working with Transients and Object Caching through WP-CLI

Ever need to quickly purge the object cache, delete a transient, delete all transients, etc.? This is a cinch from the command line.

Purge the cache…

wp cache flush

Set a value to the object cache…

wp cache set some_key some_value some_group 200

Delete a transient called “your_transient_key”…

wp transient delete your_transient_key

Delete only expired transients…

wp transient delete --expired

Delete all transients…

wp transient delete --all

Other Fun Tricks

There’s a very helpful theme unit test data file that can be downloaded and imported through the WP Admin > Tools > Import functionality, which can be used to test all kinds of scenarios that might break your theme design. This data is super easy to import via WP-CLI like so…

wp plugin activate wordpress-importer
wget https://raw.githubusercontent.com/WPTRT/theme-unit-test/master/themeunittestdata.wordpress.xml
wp import themeunittestdata.wordpress.xml --authors=create

Get a listing of all the current media sizes based on your theme/plugin set up…

wp media image-size

...

+----------------+-------+--------+------+-------+
| name           | width | height | crop | ratio |
+----------------+-------+--------+------+-------+
| full           |       |        | N/A  | N/A   |
| post-thumbnail | 1200  | 9999   | soft | N/A   |
| large          | 1024  | 1024   | soft | N/A   |
| medium_large   | 768   | 0      | soft | N/A   |
| medium         | 300   | 300    | soft | N/A   |
| thumbnail      | 150   | 150    | hard | 1:1   |
+----------------+-------+--------+------+-------+

Search for strings or regex patterns in your database…

wp db search

Want to update your version of WordPress to the nightly version for beta testing, etc.?

wp core update --version=nightly

Regenerate missing thumbnails…

wp media regenerate --only-missing

Say, for example, you’ve obtained a database dump from a production WordPress site and want to set it up locally. Once imported you can run the following command to replace all instances of the production site’s url with your local one, going through all tables (even those not registered to $wpdb). Running this command with the dry-run flag will show you what will happen, but not actually modify any data. There’s a lot of power in this command, so making sure you understand what will happen is important.

wp search-replace somesite.com somesite.local --all-tables --dry-run

Many of the commands shown above have numerous parameters available to them, and we’ve just scratched the surface as to what can be done with WP-CLI. WP-CLI is an extremely power tool at your disposal. Once you give it a try and start using it, you won’t turn back.

Looking for an exemplary WordPress development agency?

We're a team of top notch engineers and designers that build blazing fast and scalable websites, applications, and software products that improve your business. What can we do for you?

WordPress Development Services Contact Us

One thought on “WP-CLI: When WordPress and the Command Line Unite

Leave a Reply

Your email address will not be published. Required fields are marked *