What is WP-CLI?
WP-CLI is the command-line interface for WordPress. What makes WP-CLI useful is the ability to perform administrative actions without actually having to load the WordPress backend. You can use WP-CLI to manage your sites in a more efficient way! You can perform actions in bulk, manage plugins and themes, search and replace your database, and more.
Before getting started, you’ll need to install WP-CLI. This guide assumes you have SSH access to the site where your site is hosted (a requirement to use WP-CLI). From there, use the Quick Start guide to jumpstart your WP-CLI experience.
What can WP-CLI do?
You’d find a shorter list when looking at what WP-CLI can’t do! I’ll cover some of the basics in sections below.
Plugin and Theme Management
WP-CLI allows you to install, activate, deactivate, and update plugins and themes. Keep in mind, when WP-CLI runs it does still load your plugins and themes. If the code in one of your plugins or your theme is triggering a fatal error, this prevents the WP-CLI command from running. You may use WP-CLI to run a command that fails because of a fatal error. In this case read the error output to see whether it’s a plugin or theme causing the issue. If it’s a plugin, add the flag “–skip-plugins” to the end of your command. Or if it’s a theme, add “–skip-themes” to the end.
Here’s a couple examples of things you can run:
$ wp plugin deactivate akismet
Plugin 'akismet' deactivated.
Success: Deactivated 1 of 1 plugins.
$ wp plugin activate akismet
Plugin 'akismet' activated.
Success: Activated 1 of 1 plugins.
$ wp plugin update ewww-image-optimizer-cloud
Enabling Maintenance mode...
Downloading update from https://downloads.wordpress.org/plugin/ewww-image-optimizer-cloud.3.6.1.zip...
Unpacking the update...
Installing the latest version...
Removing the old version of the plugin...
Plugin updated successfully.
Disabling Maintenance mode...
Success: Updated 1 of 1 plugins.
+----------------------------+-------------+-------------+---------+
| name | old_version | new_version | status |
+----------------------------+-------------+-------------+---------+
| ewww-image-optimizer-cloud | 3.6.0 | 3.6.1 | Updated |
+----------------------------+-------------+-------------+---------+
$ wp theme list
+-----------------+----------+--------+---------+
| name | status | update | version |
+-----------------+----------+--------+---------+
| madhat | active | none | 1.0.5 |
| twentyfifteen | inactive | none | 1.8 |
| twentyseventeen | inactive | none | 1.3 |
| twentysixteen | inactive | none | 1.3 |
+-----------------+----------+--------+---------+
Database management
WP-CLI can also help manage your database. Some functions it can handle include: setting and deleting transients, search and replace, import and export databases, run queries, optimize tables, and manage your wp_options table. WP-CLI will use the database credentials found in your wp-config.php file to communicate with the database. With that in mind, be sure you confirm you have the right credentials in the wp-config.php file before running database commands!
Here are some examples of database functions you can run:
$ wp transient delete --all
Success: 5 transients deleted from the database.
Warning: Transients are stored in an external object cache, and this command only deletes those stored in the database. You must flush the cache to delete all transients.
$ wp db query "SELECT ID FROM wp_posts WHERE post_name LIKE '%database%';"
+----+
| ID |
+----+
| 57 |
+----+
$ wp db export mysite.sql
Success: Exported to 'mysite.sql'.
WordPress core
Using WP-CLI you can also manage WordPress core files. You can check the current version of WordPress, install WordPress core, update your version, revert back to a specific version, convert to Multisite, manage the wp-config.php file, and even verify that WordPress core matches checksums. When reverting down to a specific WordPress version, you’ll need to make sure to add the “–force” global flag.
Below are some examples of WordPress core-related WP-CLI commands:
$ wp core version
4.8.1
$ wp core update --version=4.8 --force
Updating to version 4.8 (en_US)...
Downloading update from https://wordpress.org/wordpress-4.8.zip...
Unpacking the update...
Success: WordPress updated successfully.
$ wp core update
Updating to version 4.8.1 (en_US)...
Downloading update from https://downloads.wordpress.org/release/wordpress-4.8.1-partial-0.zip...
Unpacking the update...
Success: WordPress updated successfully.
Manage cron jobs
If you use WP-CLI, you can manage scheduled events on your site easily, without needing an extra plugin. You can check what events are scheduled, manually execute cron jobs, verify the status of WP-Cron, and delete cron jobs.
Here are some examples:
$ wp cron event list
+------------------------------------------+---------------------+-----------------------+------------+
| hook | next_run_gmt | next_run_relative | recurrence |
+------------------------------------------+---------------------+-----------------------+------------+
| jetpack_display_posts_widget_cron_update | 2017-08-15 22:56:23 | 2 minutes 49 seconds | 10 minutes |
| jetpack_sync_cron | 2017-08-15 22:56:30 | 2 minutes 56 seconds | 5 minutes |
| jetpack_sync_full_cron | 2017-08-15 22:56:30 | 2 minutes 56 seconds | 5 minutes |
| jetpack_clean_nonces | 2017-08-15 23:06:23 | 12 minutes 49 seconds | 1 hour |
| jetpack_v2_heartbeat | 2017-08-15 23:06:30 | 12 minutes 56 seconds | 1 day |
| jp_purge_transients_cron | 2017-08-15 23:08:19 | 14 minutes 45 seconds | 1 day |
| wp_scheduled_delete | 2017-08-16 03:33:54 | 4 hours 40 minutes | 1 day |
| abtf_cron | 2017-08-16 03:47:30 | 4 hours 53 minutes | 12 hours |
| wp_scheduled_auto_draft_delete | 2017-08-16 04:30:27 | 5 hours 36 minutes | 1 day |
| mc4wp_refresh_mailchimp_lists | 2017-08-16 05:09:00 | 6 hours 15 minutes | 1 day |
| wp_version_check | 2017-08-16 08:39:43 | 9 hours 46 minutes | 12 hours |
| wp_update_plugins | 2017-08-16 08:39:43 | 9 hours 46 minutes | 12 hours |
| wp_update_themes | 2017-08-16 08:39:43 | 9 hours 46 minutes | 12 hours |
| wpseo-reindex-links | 2017-08-16 13:21:40 | 14 hours 28 minutes | 1 day |
| ao_cachechecker | 2017-08-16 15:54:22 | 17 hours | 1 day |
| ewww_image_optimizer_site_report | 2017-08-16 16:01:40 | 17 hours 8 minutes | 1 day |
+------------------------------------------+---------------------+-----------------------+------------+
$ wp cron event run wp_version_check
Executed the cron event 'wp_version_check' in 2.054s.
Success: Executed a total of 1 cron event.
$ wp cron test
Success: WP-Cron spawning is working as expected.
Manage media and posts
On top of general WordPress, database, and plugin/theme management, you can use WP-CLI to also manage individual media, posts and post types, and importing/exporting WordPress site data via XML. If you’re missing specific image sizes, you can regenerate the thumbnails associated with any image. Be forewarned, if you have a lot of images this may take a long while!
Below you can find some examples of media and post management:
$ wp media regenerate --only-missing --yes
Found 116 images to regenerate.
1/116 No thumbnail regeneration needed for "deactivate_activate" (ID 160).
2/116 No thumbnail regeneration needed for "wp_plugin_list" (ID 159).
3/116 No thumbnail regeneration needed for "pexels-photo-190574" (ID 157).
[...]
115/116 No thumbnail regeneration needed for "cropped-17361942_10155125372797938_2032688595763223584_n.jpg" (ID 6).
116/116 Regenerated thumbnails for "17361942_10155125372797938_2032688595763223584_n" (ID 5).
Success: Regenerated 116 of 116 images.
$ wp post create --post_type=page --post_title="This is an example" --post_status="draft"
Success: Created post 161.
$ wp post delete 161
Success: Trashed post 161.
Manage users
Managing users can also be accomplished with WP-CLI. When you use WP-CLI, you can add new users, add capabilities and roles to users, add new user roles, change passwords, and import a list of users from CSV.
Here’s some example user management commands:
$ wp user create test [email protected] --role=subscriber
Success: Created user 3.
Password: **********
$ wp user delete test
--reassign parameter not passed. All associated posts will be deleted. Proceed? [y/n] y
Success: Removed user 3 from http://techgirlkb.guru.
Manage Multisite networks
If you manage multiple subsites in a Multisite network, you might run into trouble differentiating the above commands to tell them which subsite to run on. For this, WP-CLI has the “–url=” command. You can use WP-CLI to run any of the commands above on each individual site by adding this global flag. Additionally, you can manage super-admins, manage Multisite-specific meta fields in the database, convert a single site to a Multisite, and more.
Here’s an example of a standard command, run on one subsite of a multisite:
$ wp plugin list --url="test.janna.wpengine.com"
+-----------------------------+----------------+-----------+---------+
| name | status | update | version |
+-----------------------------+----------------+-----------+---------+
| akismet | active-network | available | 3.3.2 |
| autoptimize | active-network | available | 2.1.2 |
| bbpress | active-network | available | 2.5.12 |
| buddypress | active-network | available | 2.7.3 |
| cloudflare | active-network | available | 3.1.1 |
| contact-form-maker | active-network | available | 1.8.38 |
| cpt-bootstrap-carousel | active-network | none | 1.9.1 |
| get-the-image | active-network | none | 1.0.1 |
| jetpack | active-network | available | 4.4.2 |
| siteorigin-panels | inactive | available | 2.4.21 |
| soundcloud-is-gold | active-network | available | 2.3.3 |
| types | inactive | available | 2.2.5 |
| wordpress-https | inactive | none | 3.3.6 |
| wordpress-mu-domain-mapping | active-network | none | 0.5.5.1 |
| wp-image-carousel | active-network | none | 1.0.2 |
| wp-smushit | inactive | available | 2.5.2 |
| wptouch | inactive | available | 4.3.9 |
| youtube-embed-plus | active-network | available | 11.5 |
| slt-force-strong-passwords | must-use | none | 1.6.4 |
| stop-long-comments | must-use | none | 0.0.4 |
| mu-plugin | must-use | none | 3.2.1 |
+-----------------------------+----------------+-----------+---------+
And there’s more!
We’ve talked about a ton of stuff that WP-CLI can manage, but it can do even more. You can combine WP-CLI commands using basic bash skills and save the output of commands. You can add aliases for commonly-used commands so they’re easier to type.
There are also several plugins that have custom-coded their own WP-CLI commands. You can find a full list in the WP-CLI handbook. And, since WP-CLI is open source, you can create your own pull request if you think you’ve identified a bug or want to add a new feature.
To sum it up, WP-CLI is an incredibly powerful tool for WordPress admins to control their sites using command line. Using the tools it provides, you can manage bulk tasks that otherwise would take hours! For a complete list of the various commands you can use with WP-CLI, check out Commands.
What do you use WP-CLI for? Have any other uses that I missed? Feel free to leave a comment or contact me.
Leave a Reply