Have you ever heard about the WP-CLI commands?
For the most part, WordPress has been built on a simple code base with a dash of object-oriented PHP as the most abstract system. However, in recent years, this has begun to change for the better. More and more developer-friendly assets are appearing, ranging from unit testing to CSS preprocessing and command-line tools. WP-CLI is one of my favourites, and we’ll look at it in this article.
What is WP-CLI?
WP-CLI is a command-line tool for developers that allows them to manage common (and not-so-common) tasks in a WordPress installation. It can add/remove users, posts, and categories, insert test data, search and replace in the database, reset passwords, assist in troubleshooting performance issues, and much more!
It has been an open-source project for over a decade, with Daniel Bachhuber serving as the primary maintainer since 2003. The primary goal of WP-CLI is to help WordPress developers speed up their workflows.
The project has evolved into so much more over the years! Other open-source projects, such as Trellis and Bedrock, are now requiring it. It officially moved to WordPress.org in January 2017 and is now co-maintained by Alain Schlesser.
WP-CLI v2 was released on August 8th, 2018, and we will look at some of the changes and new features. WP-CLI v2.0.1 is installed by default on all of our servers; if you are a WordPress user, simply SSH into your server to get started. All of our hosting plans include SSH access (Can’t connect via SSH? Fix the SSH “Connection Refused” error).
PHP 5.4 is now the minimum PHP requirement for WP-CLI v2.0.0. While this is a positive step forward, we recommend using a supported version of PHP, 5.6 or higher. For security and performance reasons, PHP 7.2 is the default on all Word installations. PHP 7.3 and 7.4 are also available.
To get started, you must first install WP-CLI, which is a very simple process. The steps for Linux and OSX are as follows run the following three commands sequentially:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
If something goes wrong, or if you’re using Windows, refer to the basic installation instructions or alternative installation methods.
After that, you should be able to issue the wp —info command and receive a meaningful response.
If you want to install WP-CLI on your server, the procedure is the same. You’re not sure which version you’re using? You can always use the wp CLI version command to determine the version.
The Basics Of WP-CLI
Having access to WordPress from the command line is powerful in and of itself, but using bash scripts can give you even more, control and speed gains.
Bash scripts enable you to execute a series of commands with a single command. If you type bash install-and-setup.sh, you will get the following output:
- Download WordPress
- Create and populate
- Create the database
- Install WordPress
- Install and activate any plugins that you require.
- Install and activate a theme
- Download and add test content
These are the steps I’d take to set up a new test environment for a project. It usually takes me 5-10 minutes, especially if there are several plugins involved. It is much faster to issue a single command.
WP-CLI Commands in General
If you’re used to working in a terminal, WP-CLI will be nothing new to you. Commands always begin with wp, followed by a command and subcommand, and then required and optional parameters, as shown below:
wp command subcommand requiredparam --optionalparam --optionalparam2=value
Install a theme to see how this works with a real-world command:
wp theme install twenty seventeen --activate
The Twenty Seventeen theme will be installed and activated on your WordPress installation.
It will only work with the WordPress installation that is currently running in the terminal. It will work with another WordPress installation if you change the directory.
In a nutshell, that was WP-CLI! While you can do some advanced things, which we’ll discuss later, you already know enough to get started and do whatever you need to. I recommend looking over the command list and trying out a few of them. We’ll look at some helpful stuff here before moving on to using WP-CLI over SSH and bash scripts.
I frequently use WP-CLI to create test environments, the first step of which is a vanilla installation. Here is a list of commands that I use:
wp core download wp core config --dbname=mydbname --dbuser=mydbuser --dbpass=mydbpass --dbhost=localhost --dbprefix=whebfubwef_ --extra-php <<PHP define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); PHP wp db create wp core install --url=http://siteurl.com --title=SiteTitle --admin_user=username --admin_password=mypassword --email@example.com
Take note of how cool this is! The first command downloads the most recent version of WordPress. The second command configures the config file with database access and some extra PHP at the end. The extra constants ensure that we have our debugging options enabled for testing. If you want to learn more about debugging WordPress, we have a detailed guide here.
The third command creates the database (it uses the database access information from the config file), and the final command instals WordPress with a few parameters.
Reinstall WordPress Core
It can also be used to reinstall the WordPress core. The command below will download WordPress core but not the default themes or plugins.
wp core download --skip-content --force
Change WordPress URL
You may need or want to change your WordPress URL for various reasons, including changing domains, moving to a subdomain, updating from www to non-www, moving files, or migrating from HTTP to HTTPS. In any case, you can easily use the wp option update command for this. As an example, consider the following:
wp option update home 'http://sample.com' wp option update siteurl 'http://sample.com'
Wrapping It Up
WP-CLI is every developer’s and website manager’s fantasy come true. As developers, we can quickly construct test sites, add test content, and perform all manner of import/export wonders. Site managers can use a single command to manage site updates and other activities across numerous sites. Take a look at the WP-CLI v2 release notes!
If you haven’t used WP-CLI yet, I strongly advise you to do so. There are also many community commands, such as WP-CLI-SSH, that provide even more great functionality! If you encounter any troubles, be sure to consult the WP-CLI common issues documentation.