We're used to managing WordPress from its web interface. However, this is not the only way and often not the most efficient either. Today we're taking a look at the pros of managing WordPress from WP-CLI and how to implement it into our server.
What is WP-CLI and what can we do with it?
A short answer to this question is found in wpcli.org
WP-CLI provides a command-line interface for many actions you might perform in the WordPress admin. ...WP-CLI provides a command-line interface for many actions you might perform in the WordPress admin...
Every developer agrees that we're all accustomed to using a command-line interface, that little black window allows us to take on complicated tasks in a quick and efficient fashion. But what can a command-line interface provide to a web content manager such as WordPress? Many if its users are not developers and the graphic user interface (GUI) displays every management task available without requiring technical knowledge.
However, what happens when we don't have acces to the GUI? For instance, if we forgot the password and the recovery procedure failed, if we installed an incompatible plugin or if we had issues after an update and our web didn't work properly. In these cases we'll have to acess the system files and/or the database directly. Without proper knowledge, the remedy can turn out worse than the issue. This is why in many cases we have no choice other than asking for an expert's help.
Managing WordPress through WP-CLI allows us to easily fix these types of incidents. We can also manage themes, plugins, widgets and users. We can also manage our content, although we might be better off using the GUI for this, save for some exceptions which we'll take a look at soon.
WP-CLI is not a plugin, it's a system of functions developed in PHP which needs to be installed independently. Let's see how we can do so.
We need to keep in mind that WP-CLI is built around Unix systems (Linux, OS X, FreeBSD). For windows, we need CygWin, although its functionality is limited. The minimum required version of PHP is 5.4, and it will not work correctly in WordPress versions prior to 3.7.
Obviously we need acces to the server console. If we're running a shared hosting service, we need to enable said access. In some cases, such as Siteground, we'll have it pre-installed when purchasing the hosting.
There are several ways of installing WP-CLI. Out of all of them, the suggested option is the simplest: download the lone wp-cli.phar file.
The easiest way to do this is by curl or wget, an example:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
verify the installation with php wp-cli-phar --info and, if everything went well, we should get a result similar to this:
OS: Linux 4.4.0-97-generic #120-Ubuntu SMP Tue Sep 19 17:28:18 UTC 2017 x86_64
PHP binary: /usr/bin/php7.0
PHP version: 7.0.32-0ubuntu0.16.04.1
php.ini used: /etc/php/7.0/cli/php.ini
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: /home/ubuntu
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.0.1
To simplify its use, we'll give execution permissions to the wp-cli.phar file, move it to the folder /usr/local/bin and, while we're at it, rename it to, say, wp. This way, we can launch it directly from any location. The commands are:
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
From now on, we just have to write the corresponding command to manage WordPress through WP-CLI. For example, wp --info will display the previous result. If we just type in wp, we'll get a list of the commands available.
In order to update WP-CLI we'll use wp cli update. If we used any other install method we'll have to redo the install to update the tool.
How to manage Worpress using WP-CLI
Let's take a look at how WP-CLI simplifies some of the more common admin tasks.
1. WordPress admin core
WP-CLI allows us to install and update the WordPress core. The command for this is wp core. To learn about every available option we can use wp help core
This example shows one of the greatest qualities of WP-CLI. We don't need to memorize every command. We'll always have detailed descriptions of them, including some examples
-- Installing WordPress
Find the installation folder and execute wp core download. Using the parameter --locale we'll choose the language (US by default) and using --version we'll select the version we want (the latest one available)
We need to create the wp-config.php file. We'll use the database connection parameters which need to be created beforehand. Here's an example:
mysql -u root -prootp -se "create database bdata;"
mysql -u root -prootp -se "grant all privileges on bdata.* to 'user'@'localhost' identified by 'db_pass';"
mysql -u root -prootp -se "flush privileges;"
In this case the command we've got to use is:
wp core config --dbname=bdata --dbuser=user --dbpass=db_pass --dbhost=localhost
Lastly, proceed with the installation
wp core install --url=http://url.blog.com --title="My blog" --admin_user=uadm
Fill the paramaters indicated above with the adequate values for our installation.
Installing through WP-CLI is just as easy as a installing traditionally, and it's much faster as well.
-- Updating the WordPress core
This task is much more common. Using wp core check-update we'll check if there is any update available and using wp core update we'll have it installed. It's commonplace for a core update to include modifications to the database. The command to update the database is wp core update --db.
It's good practice to make a backup of the database before any update. In WP-CLI we can do this with wp db export copy-db.sql. This command makes a copy inside the root folder of the site.
We can run a specific version with wp core update --version=xxx. This is extremely useful if we need to revert back to a previous version to solve any compatibility issues with any installed themes or plugins. In this case, we might need to add the parameter --force.
Without WP-CLI, our other options would be to make use of a plugin or attempt a rather complicated manual process. Those who have gone through a similar situation will appreciate the pros of managing WordPress through WP-CLI.
2. Adminis themes and plugins
How many times have we installed a plugin and found out too late that our website stops working and our admin panel is inaccessible. In such cases we need to access the server and delete the incompatible plugin folder.
WP-CLI makes it easy with wp plugin. These are some of the available options for this command:
- wp plugin deactivate plugin-name: Deactivate the specified plugin
- wp plugin delete plugin-name: Delete the folder of the specified plugin
- wp plugin update plugin-name: Update the specified plugin to the latest version available
- wp plugin status: Display a list with every installed plugin
In order to delete an incompatible plugin we simply have to use wp plugin delete stating the name of the plugin and our website will be running as normal. It's that simple.
We could also update every plugin all at once using wp plugin update -all. We can combine commands as well, for instance: if we wanted to delete every inactive plugin we would use the following command: wp plugin delete $(wp plugin list --status=inactive --field=name).
Managing themes is done in the same way, except that the command in case for this is wp theme. For example, the output wp theme status would give should look like this:
3. Admin users
The parameters used here are:
- user: Name, ID or e-mail for the user of their choosing
- display_name: Username displayed
- user_pass: User's password
This method is way better than the usual method of directly accessing the database and entering the new password hash.
Another usual functionality is deleting users. With WP-CLI, we can reassign entries from users to other users in the same order: wp user delete user --reassign=other-user-ID.
These types of tasks in WordPress are managed through WP-CLI more productively and without overloading our website with plugins.
4. Create child themes and custom content types
We've already discussed the importance of using child themes in WordPress. If we don't want to use plugins nor creating the folders manually, then WP-CLI will come and assist us. Again, a simple line of text will solve the issue:
wp scaffold child-theme child-theme --parent_theme=parent-theme --theme_name=theme-name
With this command we'll have a new theme up and ready to be run. We have more settings available to us, such as --activate (activate immediately) and --force (overwrite an existing theme). We can also provide our own info as authors.
On another hand, the command wp scaffold can be used to generate custom entry types, taxonomies and a basic plugin structure.
5. Regenerate miniatures and manage media
As we would expect, wp media is the command to use for these tasks. Out of the many options available, the more interesting one might be wp media regenerate --yes. With this command we have quickly created miniatures for every image size that is registered in our site. The parameter --yes saves us from having to answer every confirmation prompt.
That aside, we can also regenerate a specific size and/or limit ourselves to specific images. We can also include at once an entire image folder to the media library.
How many times have we changed theme or added a plugin only to be met with images which are not displayed properly? We could always use a plugin to solve the issue, however we have a much more elegant solution available to us. This is another pro of managing WordPress through WP-CLI.
6. Leave blank space
Esta es una opción muy útil, por ejemplo, hemos finalizado la implementacion y antes de pasar a producción deseamos borrar todo el contenido de pruebas. Lógicamente nos interesa mantener las configuraciones y opciones en nuestro tema y plugins activos. This is a very useful feature. Take this example, we've finished the installation and before heading to production we wish to delete all test contents. Logically, we would like to keep configs and settings for our active themes and plugins.
WP-CLI provides, once again, an easy solution with wp site empty. If we add the parameter --uploads, the content inside the folder will be deleted. And as always, --yes will prevent us from answering every confirmation prompt.
We've seen a few simple examples of how to manage WordPress through WP-CLI. If we've found ourselves in similar situiations, we'll know full well the pros of using the tool. Its ease of installation (basically uploading a file to our server) and its help feature which saves us from memorizing every command and setting, make this tool almost essential to any WordPress installation.
We've only scratched the surface here. Those who have work experience with the command console will find many more opportunities to be productive in their day to day tasks.