Difficulty: Intermediate

For those who like to use the command line interface will be happy to know there is a tool with WordPress that you can use called WP-CLI.  With WP-CLI you can control your WordPress site through the command line interface with ease.  Some of the commands are extremely useful and improve your WordPress experience.  With more and more hosting companies having WP-CLI installed by default now is the best time to learn how to use it!

Table of Contents:

Introduction

In the tutorial we will be going over a couple of useful WP-CLI commands that you may use in an average day.  A basic knowledge of command line interface is recommended and knowing your way around a terminal.  I will show how to install WordPress, create users, work with themes and plugins, and a little bit more.  I will not be going over everything WP-CLI offers, but I will update the article as needed.

WP-CLI works by starting with wp followed by the type of command you want to initiate.  You follow it by a sub command, and then any arguments you want.  Throughout the tutorial I will be breaking down each command to further elaborate this.  Since I will not have every command in this tutorial, please reference https://developer.wordpress.org/cli/commands/ for more commands and some explanation on how they work.

wp core

wp core is used to handle core related functions.  Such as installing and updating WordPress.  We will go over how to do an install, update, and downgrade in this section.  To learn more about the wp core command, check https://developer.wordpress.org/cli/commands/core/

Installing WordPress

Yup!  You can very easily install WordPress using WP-CLI!  You can do a full installation or just download the WordPress files and go through the the web interface to install.  In this section we will go through the whole installation process.  I believe you will find it easy and fast!

We start with downloading the WordPress files.  The command to do this is wp core download.  This will download the current version of WordPress.  Breaking up the command we have the command wp core and the sub command download.

If you wanted to, you could specify the version of WordPress you want by adding the argument --version=$version_number with $version_number being the version you want.  From here you can visit the URL and go through the the web interface for the installation if you would like.

core downloadBefore we can install WordPress, we will need to create the wp-config file.  To do this in WP-CLI you would use the command wp config create followed by whatever arguments you want.  The basic ones you need are the database.  Since bash records everything you type, there is a way to add the database password without it being typed out.  Using the argument --prompt I can have the password in a txt file and have it pushed to the dbpass argument.  To learn more about wp config create, visit https://developer.wordpress.org/cli/commands/config/create/

wp-config createNext we get to the installation process.  wp core install will start the installation. We have some arguments here such as the site URL, site title, and admin user information. If you would like, you can specify a password for the admin user, or have it randomly create one.

Visit https://developer.wordpress.org/cli/commands/core/install/ for more information

core installThat's it!  Pretty straight forward to get a WordPress site up and running!

Updating WordPress

The next task we will do is update WordPress.  This is done by typing wp core update. In this command, wp core is the command and update is the sub command.  That is all you have to do to get WordPress updated.  There are no arguments that need to be added to this.

core updateDowngrading and Version Checking WordPress

It happens, sometimes we need to downgrade WordPress.  Maybe a plugin or the theme was not compatible with the newer version.  Well, wp-cli makes it extremely easy to get WordPress back to the version that you need!

We start with the same command to update WordPress, wp core update. However, we add some arguments to make it grab the version of WordPress we want, which is the --version argument. We then add --force to force the operation to execute. If we do not add this WordPress will not go to that version, because it will already be up to date.

Finally, we have a little bonus argument and command.  The --quiet argument is not required, but it will supress the output of the command. Then we have wp core version which just shows us the version of WordPress we are running.  To learn more about wp core update, check out https://developer.wordpress.org/cli/commands/core/update/

downgrade WordPresswp plugin

Managing your plugins with wp-cli is amazing!  You can install, activate, deactivate, and uninstall them.  In this section we will go over a couple of installation methods, updating, and removing.  To learn more about wp plugin visit https://developer.wordpress.org/cli/commands/plugin/

When working with plugins in WP-CLI, you need to use the slug name.  The slug name is easily found in the URL of the plugin over at the WordPress.org plugin repository. For example, YOAST, a popular SEO plugin with the URL of https://wordpress.org/plugins/wordpress-seo/, slug would be wordpress-seo. 

Searching and Installing

Another way to find the slug is to run a plugin search using WP-CLI.  We have the command wp plugin followed by the sub command search and then the keyword we are searching for.  If you need to search for more than one word, you will need to hyphenate it.  Next we will go over the installation method.

To install a plugin from the WordPress plugin repository you use the command wp plugin followed by the sub command install and then the slug name. Optional, but highly recommended is to add the --activate argument to activate the plugin after it installs.  If you do not, you will just have to run the command wp plugin activate $slug_name where $slug_name is the name of the plugin.

plugin search and installYou can even install local zip file using WP-CLI.  Just use the command and sub command wp plugin install followed by the path of the zip file on the server.

plugin install localAnother great thing you can do is install multiple plugins at once. All you need to do is add the slug names one after the other. 

multiple pluginsUpdating and Downgrading

Updating a plugin is just as easy as it is to update core WordPress.  Using the command and sub command wp plugin update. You can then either specify which plugin you want to update by adding the slug name, or just use the argument --all to update all the plugins.

update pluginsSimilar to downgrading WordPress, there are times were we might need to downgrade our plugins.  To do that we would use the command and sub command wp plugin update followed by the slug name and the --version argument with the version we would like to use.

plugin downgradewp theme

Working with themes is the same as working with plugins.  So, a lot of this will be a repeat of the plugin section.  You can learn more about the theme command here: https://developer.wordpress.org/cli/commands/theme/

Searching and Installing

To install a theme from the WordPress theme repository you use the command wp theme followed by the sub command install and then the slug name. Optional, but highly recommended is to add the --activate argument to activate the theme after it installs.  If you do not, you will just have to run the command wp theme activate $slug_name where $slug_name is the name of the theme.

theme search and installJust like with plugins, you can install a theme zip file using wp theme install followed by the location of the zip file.

theme install localwp user

Working with users can be time consuming.  Luckily, with WP-CLI it's a breeze to work through.  We will go over how to create, remove, and change roles for users.  To learn more about the user command check out: https://developer.wordpress.org/cli/commands/user/

Creating Users

Creating a user is pretty straight forward.  You start with the command wp user followed by the sub command create. You then add the name and email of the user. If you want to give them a role outside of subscriber you would use the --role argument.

As an extra benefit, using the --porcelain argument will hide the password from being displayed in the prompt.

user createYou can even generate a bunch of random users if you would like.  Use the generate sub command and then the argument --count to specify how many users you would like created.  We also see another use of wp user, and that is the list sub command.  This will give you a list of all the users for the site. 

user generateReassign Roles

To reassign a role, just use the set-role sub command followed by either the username or the ID and what role you would like them to have.

change roleRemoving Users

To remove a user you just have to use the delete sub command. You then want to follow it up with the username or ID of the user you want to remove. Then, a good idea is to use the --reassign argument. This argument will reassign any created content to another user.

delete user

wp media

The wp media command is one of my favorite wp-cli tools.  The reason why is because you can use it to do things like regenerate thumbnails and import images that you may have used SFTP to upload to WordPress.  To learn more about wp media, check out https://developer.wordpress.org/cli/commands/media/

Importing and Regenerating

As I said above, we sometimes have images that either went missing or that were uploaded through SFTP instead of using the media uploader.  It can be a pain to work with them, because then they do not show up in the media section of WordPress.  Luckily, there is a WP-CLI command that can fix that right up! import and regenerate.

With import, you want to specify the directory where you want to import the file(s) from.  In the example below there is an asterisk, which is a wild card for all.  You want to be careful when using that as duplicates can occur.  It is best to use the full path if you just need a few images imported.  

The regenerate sub command is a bit smarter in how it operates.  It will know if there are not any missing thumbnails and regenerate them for you.  If you have quite a few to regenerate, you will want to use the --yes argument which will auto accept all regeneration.

media importConclusion

WP-CLI is a powerful tool that can expand your WordPress productivity.  Obviously, there is a lot more commands then the ones listed here.  Expect this page to be updated with useful/new commands as they come.