WP-CLI – Manage WordPress Websites Better and Faster

WP-CLI allows you to manage websites using simple commands without needing to navigate trough many pages in WordPress administration. Whether you are a developer or administrator, WP-CLI can save you a lot of time and even get inaccessible website back to life.

What Is WP-CLI?

WP-CLI is command line interface for WordPress. That means that it defines commands, that you can execute on your server, that you can use to performs common actions. Tasks like managing posts, users, plugins, themes and website options can be achieved in the command line. We will have a look at some practical use cases in this article.

How Can You Use WP-CLI?

WP-CLI is not included in WordPress installation by default. Luckily a lot of WordPress hosting providers have already adopted this tool and made it accessible to its users. You can find out if this feature is included in details of your hosting plan or you can contact your hosting provider. WP-CLI is also included in Trellis that we use to manage servers for our clients at Zeni.

If you find out that you do not have access to WP-CLI on your server,  assuming you have SSH access to your server or you want play with WP-CLI locally on you computer, you can get it working using just few commands. On the OS X and Linux this can get you to fully working WP-CLI in no time:

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 you use Windows, or you are interested in different installing methods you can have a look at WordPress Documentation.

Speed up Time Consuming Tasks With WP-CLI

There are many administrative tasks in WordPress, that require you to perform multiple repetitive actions to get you to your goal. That might include a lot of page loads and clicks and take you couple of minutes in some case maybe hours. Thanks to WP-CLI, you can perform these tasks lot of easier. Especially bulk actions like for example managing multiple plugins or media can be speed up with WP-CLI.

Install Multiple Plugins at Once

Are you just starting with the fresh site and you want to install multiple plugins that you regularly use or you just received request to install few new plugins? Using WP-CLI this will only take you to write single command. Following command installs 2 plugins and activates them thanks to activate flag:

wp plugin install bbpress classic-editor --activate

This command also allows for passing zip archives with the plugins, for example here below. Local paths are supported too:

wp plugin install https://example.com/plugin.zip

Using wp plugin command can achieve much more, for example updating all the plugins on your website. I highly recommend to have a detailed look at the documentation of wp plugin command.

Bulk Import Media

You can easily import multiple media using simple command. Here we are going to import all JPG filesin the user pictures folder. This will copy the files from the original folder and automatically register them as attachments:

wp media import ~/Pictures/**\/*.jpg

 If you are interested in more media oriented commands you can have a look to command wp media.

Managing Users

wp user  command helps with all of tasks related to managing users. Here is an example for creating user daniel with mail address [email protected] and role administrator. I have used this exact command countless times when there was new project that i have worked on:

wp user create daniel [email protected] --role=administrator

Managing user passwords, roles, capabilities and more is very convenient thanks to WP-CLI. If you are interested in user management, here you can find more about command wp user.

WP-CLI and Dealing With Technical Issues

WP-CLI is great servant when you are trying to deal with technical issues as we are going to see in next few lines. 

Debugging Theme and Plugin Issues

Imagine you have just activated faulty plugin, which causes WordPress administration to be inaccessible. WP-CLI can rescue you in this situation using single command:

wp plugin deactivate faulty-plugin

Similarly you can activate different theme using WP-CLI if you believe that current theme is causing trouble. Following example activates chosen theme:

wp theme activate different-theme

Manipulating Options

Do you need to change values of some options? This can be useful when you want to debug an issue or when changing some options is not directly possible in the administration. In this example we are going to change current admin email:

wp option update admin_email [email protected]

You can find more about managing options in the documentation of wp option command.

Managing Database

WP-CLI is also great for managing database. You can easily create backup of current database and save it to current user directory using following command:

wp db export ~/backup.sql

This is extremely useful when you want to create export of the database for use on staging or local development environment or to create a backup before performing some risky action like updating database structure because of some plugin. If anything goes wrong, we can easilygo back using this command:

wp db import ~/backup.sql

If you are planning to experiment we highly recommend to not to do it on your customer facing production environment. That is what staging server can be used for. You can learn what staging environment is in our article Seven reasons to use staging environment for your project on WordPress. If you are interested in creating staging environment, you can learn about this in our article  How to create staging environment?

Another useful feature is ability to quickly enter your database command line interface to execute SQL using following command:

wp db cli

Replace All the Occurrences of Some Value

Imagine site moved to different URL, or you want to use your production database on the staging server. After you import database to new server you are quickly going to find out that some of the links are not working and that media files are not visible. That is because unfortunately WordPress uses absolute URLs in many cases. To fix this issue WP-CLI have very useful command known as wp search-replace. With this command you can replace all the old URLs with new ones very simple:

wp search-replace https://old-website.old https://new-website.new --dry-run

The dry-run flag on the end is very useful here because it runs the entire search/replace operation with showing the report, but no changes are saved into the database. It is definitely a good idea to run this command in dry run mode first and after reviewing the report running it without dry run mode.

Generate Dummy Content With WP-CLI

This one is very useful for developers, who need some mocked content on the site for development purposes. For example, you can generate tens of posts, users, terms and comments using just few commands. Here are few examples:

wp user generate --role=subscriber --count=50
wp post generate --count=200 --post_type=post
wp post generate --count=20 --post_type=page
wp term generate category --count=10
wp comment generate --count=10 --post_id=123

This will give you a site with 50 new users, 200 posts, 20 pages, 10 categories and our selected post already have 10 comments on it. This simulates active site, that have been around for some time and we did not need to do anything manually. We can start working on developing WordPress theme or plugin more easily.

There Is a Lot More to Achieve

We have introduced many use cases of the WP-CLI in this article, but that is far from all that is possible using this tool. To became familiar with all the possibilities, there is no better source than WordPress documentation. I hope that WP-CLI will help you to become better developer or administrator.