What You Need to Know about the WordPress Command Line Interface
Web developers are widely using command line interfaces (CLI) to perform tasks more efficiently, configure their machine, or access features not available through a graphical user interface (GUI). Since we haven’t written about one of Alternative-spaces’ favorite technologies for a while, we dedicate this post to the WordPress console interface.
Although the platform has made comment management, updates, post scheduling, and other tasks simpler, they still take time even for experienced users. The WordPress Command Line Interface, or simply WP-CLI, empowers them to handle the entire WordPress site right from the server’s command line. WP-CLI v2.3.0 should be released in August 2019, intending, as always, to speed up the developer’s workflows.
This article will first explain how to install the tool, then give examples of the WP-CLI use, and list the most valuable CLI commands. If you’re tired of jumping between dashboard tabs to manage your WordPress site, you even don’t need to be a programmer to get the most out of it.
Getting Started with the WordPress Command Line Interface
You can install WordPress CLI on MacOS, Microsoft Windows, and Ubuntu. WP-CLI’s requirements for the hosting environment include:
- A UNIX-like environment;
- At least PHP 5.4 (we recommend 5.6 or later versions);
- WordPress 3.7 or later;
- Secure Shell (SSH) access to your hosting environment.
Usually, if you need SSH access, you’ll have to use a Virtual Private Server (VPS). If you’re using a UNIX-based system, you can use SSH to connect to a remote server via your native command line. Windows users will have to download an SSH client (Putty is recommended).
There are many ways to install WP-CLI, but the recommended method involves downloading the Phar build. The steps for Linux and OSX are as follows:
1) Access your server’s command line via SSH and download wp-cli.phar using curl or wget. For example:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
This will download the wp-cli.phar file to your root directory.
2) Make the file executable using this command:
chmod +x wp-cli.phar
Place the wp-cli.pharfile on your PATH, to a new directory called ‘wp’:
sudo mv wp-cli.phar /usr/local/bin/wp
That way, you can call up the application by typing wp into your command line. For example, the wp command allows you to type wp –info instead of php wp-cli.phar –info.
3) Test your install. If WP-CLI is installed successfully, the output after running wp –infoshould look like this:
OS: Linux 4.10.0-42-generic #46~16.04.1-Ubuntu SMP Mon Dec 4 15:57:59 UTC 2017 x86_64
Shell: /usr/bin/zsh
PHP binary: /usr/bin/php
PHP version: 7.1.12-1+ubuntu16.04.1+deb.sury.org+1
php.ini used: /etc/php/7.1/cli/php.ini
WP-CLI root dir: /home/wp-cli/.wp-cli
WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
WP-CLI global config: /home/wp-cli/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 1.5.0
If you’re on Windows, use one of the other installation methods. In the end, you should still be able to issue the wp –info command and get a meaningful response.
WP-CLI installed using the Phar method is the easiest to update. You can run wp cli update at any time or wp cli update –nightly to use the latest nightly build of WP-CLI.
WP-CLI comes with a tab completion script for Bash and Z shell. Download wp-completion.bash and source it from ~/.bash_profile:
source /FULL/PATH/TO/wp-completion.bash
Then, run source ~/.bash_profile.
Check out the quick start guide for a short introduction and some example usage. Some say the WP-CLI tool is not suitable for the average user, and that efficient use of the utility requires at least familiarity with the command line and a slight learning curve. Others claim it’s totally possible to learn without ever leaving the command line itself.
Anyway, what is CLI for WordPress all about?
The Benefits of the WordPress Command Line Interface
WP-CLI is all about efficiency. It simplifies the interaction with the WordPress platform. The process includes commands for most of the WordPress tasks that developers encounter daily and dozens of parameters to fine-tune them if necessary.
You can even install WordPress from command line, i.e. using WP-CLI rather than the usual WordPress GUI. Some find the method more streamlined.
Here’s how you can do it on a shared cPanel hosting account. Basically, you would have to enable SSH access for your account and connect to the server via SSH. Create the project directory that you want to install WordPress in. Navigate to this folder, grab the WordPress core files, and add them to the directory with the wp core download–version=5.6 command. Create a new database and database user, and add the user to the database with all privileges. Change the database name to the one that you’ve created for the WordPress site. Then, set up the config file:
wp core config –dbname=wordpress –dbuser=root –dbpass=root
Wait for the confirmation message: ‘Success: WordPress downloaded.’ Finally, install WordPress via the command line with a single command:
wp core install –url=”your_domain” –title=”Blog Title” –admin_user=”admin username” –admin_password=”enter_your_password” –admin_email=”enter_your_email”
If your install went well, the wp core versioncommand should return a single line indicating which version of WordPress you’re running.
Access to WordPress via the command line is powerful in itself, but you can gain even more control and speed if you use bash scripts. (Bash scripts allow running a sequence of commands with a single one.)
For example, Alternative-spaces’ web developers use the WP-CLI capabilities to automate routine processes at the initial stage of a WordPress project development. We wrote a bash script for internal use which enables us to automatically generate and deploy locally a WordPress project that is ready to be developed. The progress from zero to 100% readiness takes a few minutes.
WP-CLI helps developers manage routine and not so common tasks of a WordPress installation. It can add/remove users, posts, categories, insert test data, search and replace in the database, reset passwords, help troubleshoot performance issues, and much more.
Another example of the WP-CLI benefits is the case of the White Screen of Death (WSOD). The critical situation can occur for many reasons, including:
- a problem when updating WordPress core, if it was completed incorrectly;
- error when installing a new plugin/theme or updating a plugin/theme;
- error when working with the database;
- problem with cached pages.
On a live website, errors are not displayed on the front-end. Even having checked the log files on the server, a developer can spend a while trying to fix WSOD. They must manually disable or remove installed plugins and themes via FTP, clear the server cache, change files and directories permissions, and so on.
Thanks to WP-CLI, this issue is usually solved by several commands running in the server console, such as:
wp db check
wp db repair
wp cache flush
wp transient delete –all
wp plugin deactivate –all
wp theme deactivate –all
WP-CLI provides access to many actions you might perform in the WordPress admin, but it also includes commands for things you can’t do there.
The tool is particularly useful for those who are developing their own WordPress plugins or themes. The following chapter contains some of the most critical commands you need for running your WordPress site.
The Most Useful WP-CLI Commands
CLI Commands for the Core, System & Database
With the following WP-CLI commands, the developers can:
- Get information about the environment version (operating system, PHP, and CLI):
wp --info
- Find out the current version of WordPress core:
wp core version
or
wp core version –extra
- Update WordPress core to the latest version:
wp core update
- Download the latest WordPress core:
wp core download
- Create a WordPress project:
wp core install –url=${WP_HOME} –title=”${WP_BLOG_NAME}” –admin_user=${WP_ADMIN_USER} –admin_password=${WP_ADMIN_PASSWORD} –admin_email=${WP_ADMIN_MAIL}
- Change the permanent links structure:
wp rewrite structure ‘/%postname%/’
- Generate the configuration file wp-config.php:
wp config create –dbname=${DB_NAME} –dbuser=${DB_USER} –dbpass=${DB_PASSWORD} –dbhost=${DB_HOST} –dbcharset=${DB_CHARSET} –dbprefix=${DB_PREFIX}
- Create a database from the data specified in the configuration file wp-config.php:
wp db create
- Check the database:
wp db check
- Query the WordPress database:
wp db query “SELECT COUNT(*) FROM wp_posts;”
- Fully export or import the WordPress database:
wp db export wordpress.sql
wp db import wordpress.sql
- Get an option from the table wp_options:
wp option get blogdescription
- Change an option in the table wp_options:
wp option update blog_public 0
WP-CLI Commands for Themes and Plugins
Installation and activation of themes via the WP-CLI are straightforward. A single command can be used to do both:
wp theme install twentynineteen –activate
The theme installcommand is used with the added activateparameter. If there’s no need to activate a theme at once, just omit this parameter.
Updating themes is simple too: you can use either of the following commands:
wp theme update twentynineteen
or
wp theme update –all
To check which themes would be updated before using the –all parameter, run it with the –dry-run parameter:
wp theme update –all –dry-run
It’ll return a list of themes that have updates available: you can update the themes either individually or simultaneously.
The following CLI commands further facilitate the work with themes and plugins by enabling developers to:
1. Get a full list of themes installed in the system:
wp theme list
2. Activate a downloaded theme:
wp theme activate twentynineteen
All commands for themes are also valid for plugins. You only have to replace the word ‘theme’with ‘plugin’in each of the commands.
3. Get a list of all installed plugins:
wp plugin list
4. Install a plugin from the WordPress.org directory:
wp plugin install tinymce-advanced
5. Activate a plugin:
wp plugin activate tinymce-advanced
You can install plugins not only from the repository but also from remote files. It’s great if you’re developing a plugin or using a premium one. For example, the following command installs two plugins from the repository and one from an Amazon S3 server:
wp plugin install advanced-custom-fields jetpack https://d2qas3txbec7n.cloudfront.net/wp-content/uploads/2019/07/myplugin.zip –activate
6. Deactivate a plugin:
wp plugin deactivate bbpress
You can name multiple plugins simultaneously or use the –all parameter. The latter can be handy if you’re troubleshooting compatibility issues and need to deactivate all the plugins at once. Later, you might go back and enable them one by one, testing as you go.
7. Update a plugin:
wp plugin update contact-form-7
8. Delete a plugin:
wp plugin delete tinymce-advanced
WP-CLI Commands for Posts, Pages, Comments & Users
The commands allow the developers to do the following quickly and easily:
- Get a list of all posts:
wp post list
- Get a list of all pages:
wp post list –post_type=page
- Get a list of all comments:
wp comment list
- Mark a comment as spam:
wp comment spam 14
- Delete a comment:
wp comment delete 15 –force
- Get a list of users:
wp user list
- Create a new user:
wp user create bob bob@example.com –role=author
- Update the details of the user with ID=8:
wp user update 8 –display_name=Mary
- Get the details of the user with ID=8:
wp user get 8
- Get the metadata of the user with ID=8:
wp user meta list 8
- Delete the user with ID=8 and reassign their posts to the user with ID=9:
wp user delete 8 –reassign=9
There’s detailed documentation for each of the parameters. You can get it by typing wp –help in the command line. It’ll return a complete list of the top-level WP-CLI commands. If you want to learn what each of them does, you’ll have to expand upon the helpcommand, e.g., wp help theme install. The list of the commands and their parameters is also available on the official developer site.
WP-CLI comes with dozens of commands, but it’s pretty easy to create a custom one. Read the commands cookbook and internal API docs to discover a variety of helpful functions you can use in your custom WP-CLI command. WP-CLI supports registering any callable class, function, or closure as a command. It reads usage details from the callback’s PHPdoc. WP_CLI::add_command() (doc) is used for both internal and third-party command registration.
To Recap
The WordPress Command Line Interface is a powerful, versatile, convenient, and developer-friendly tool that helps improve productivity during WordPress web development.
The benefits of the WP-CLI are numerous. Start with the ability to interact with your WordPress installation on your server from the command line. WordPress platform has made it quite simple to install and manage the plugins, but for those who are used to the command line, the WP-CLI tool is just perfect. WP-CLI commands empower the developers to update WordPress core, set the necessary initial settings for the site, install, activate, deactivate, and update themes and plugins, and manage the users’ access to the website, their posts, and comments quickly and conveniently. Developers can quickly create test sites, add test content, perform database search-replace operations, and do every kind of import/export magic. They can configure multisite installations and much more without using a web browser.
WP-CLI is efficient for managing your WordPress-based properties and could be ideal if you’re running multiple sites on the same server. Site managers can handle site updates and other tasks for multiple sites with single commands.
If you need any assistance with a WordPress site, contact our expert team!
Content created by our partner, Onix-systems.