Manage WordPress websites with WP-CLI commands

Gijs Hovens, February 4, 2019

Save time when developing and maintaining websites by using the command line, specifically the command line tool WP-CLI. Using this tool, you can add users, install plugins, adjust settings and more, all of it much faster than by using wp-admin. In this blog, we explain exactly what WP-CLI is, how to use it and which commands are our favorites.

What is WP-CLI?

The acronym WP-CLI stands for WordPress – Command Line Interface. As suggested by its name, WP-CLI is a tool for managing WordPress from the command line. The command line is a way to manage something by typing command in a text environment (Command Line Interface, or CLI) instead of, for instance, clicking your mouse in a graphical environment (Graphical User Interface, or GUI). Command line is also known as ‘shell’.

Using WP-CLI, you can manage your WordPress site using various commands. This includes installing, activating and updating plugins or themes, managing users or carrying out search and replace actions in the WordPress database.

The first version of WP-CLI was written in 2014 by Christi Burca (@scribu). Later, the project was taken over by Daniel Bachhuber. Daniel shared his slides of a WordPress VIP WP-CLI workshop, very interesting for experienced developers. WP-CLI is currently maintained by Alain Schlesser. Read the interview we had with him at WCEU 2018.

How do I work with the command line at Savvii?

At Savvii, you connect to the command line on your VPS using the SSH protocol. SSH stands for Secure Shell. You can activate SSH at our control panel.

SSH Access at Savvii

Next, log in at the command line using a program like PuTTy (Windows) or Terminal (Mac). You won’t start off at the folder containing your WordPress right away, but two levels higher instead. This folder contains various folders with things like logs, custom configuration as well as your WordPress site. You navigate to that folder using the command:

cd wordpress/current

Once in that folder, you can continue to manage WordPress using WP-CLI commands. Simply knowing the WP-CLI commands is not enough to work on the command line comfortably. We advise you to get to know the basic Linux commands as well.

It is best not to take your first steps at the command line and WP-CLI in a production environment, but rather in a staging environment. Because commands at the command line don’t offer an undo or autosave function, nearly all of your actions are irreversible. This means it’s best to do some dry runs first.

At Savvii, SSH and WP-CLI are available with VPS plans, while on staging sites they are offered as part of a VPS plan.

How does WP-CLI work?

All WP-CLI commands start with ‘WP’, followed by multiple subcommands and possibly some arguments. An example is

wp plugin install bbpress --activate

WP is the command for WP-CLI, plugin install are two subcommands, bbpress is a ‘positional argument’, and -activate is an ‘associative argument’. This sounds confusing, but the examples at wp-cli.org provide you with a lot of guidance without having to worry about the type of argument.

The command line automatically provides you with clarification when you enter an incomplete command. Typing

WP help

is an option as well.

Our favorite WP-CLI commands

Adjust Home & site URL

wp option update home 'http://example.com'

wp option update siteurl 'http://example.com'

The WP option update is a favorite of our support colleague Bas. Adjusting the home and site URL is often required when moving WordPress sites, and when running a WordPress site via https.

Running find & replace

wp search-replace 'example.dev' 'example.com' --skip-columns=guid

This one is my personal favorite. Like the previous one, it is very useful when moving websites or when you would like to change your website’s domain. An adjustment of the home and site URL is often not sufficient to convert all internal links to your new domain. The argument –skip-columns=guid ensures that the value for the GUID is not changed in the database. It is important that the GUID is unique and unchanging. Please see the WordPress codex for more information about the GUID.

It’s a good idea to run this command using the argument -dry run first. By doing so, you don’t change anything yet, but you’ll be able to see how many items will be changed.

View plugin list

wp plugin list

This command provides you with an overview of all installed plugins, their status (active or inactive), whether they are up to date, as well as their version numbers. Of course, it is the first step in cleaning your inactive plugins and updating all of your plugins to their latest versions. All of it can be done using WP-CLI. View all WP-CLI commands for plugins. The argument -all is convenient when updating or deactivating.

View user list

wp user list

The user list command, similarly, is the starting point for cleaning all of your users and adjusting their roles according to the least privileges principle.

Generating test data

There will be times when you would like to test a WordPress site, for instance regarding its performance. When using a (nearly) empty WordPress site in these cases, your test will not yield any valid results. Using WP-CLI, you can fill your test website with fake data for aspects like posts, terms and users. A few examples:

To create 3 users with the role author:

wp user generate --count=3 --role=author

To create 10 random posts:

wp post generate --count=10 --post_type=page

For the specific commands, please see the instructions for post and user commands.

Extra tip: combine wp post create with a lorem ipsum generator for fake content.

View WP cron event list

You already saw that we consider a tidy WordPress site to be important. It is safer as well as faster. Sometimes, there will be some heavy tasks in the wp cron. By occasionally checking the wp cron list, you will notice if something is included that might no longer be necessary or could be done less often.

wp cron event list

Verifying checksums

Verifying the WordPress core checksums is our colleague Ferdi’s favorite. This command checks whether all files in your WordPress core are still original. If this is not the case, you have either changed it yourself (which is highly inadvisable because it will break your site during an update), or you have been hacked.

wp core verify-checksums

Would you like to start working with SSH and WP-CLI? Activate SSH in your VPS plan or contact us in order to upgrade your package to a VPS.

Leave a reply