WP-CLI Tutorial

Tutorial on How to Use WP-CLI

WP-CLI is a command line interface which allows the users to manage their WordPress web sites from the command prompt. Upgrades can be performed, backups can be generated, new posts can be published and most of the regular admin actions can be performed with a set of commands.

This tutorial explains how to use the WP command line interface in order to complete regular administrative tasks like upgrades, database backup creation, plugins and themes installations and removals, publishing and deleting posts, changing site's URL settings and getting help on chosen commands. Note that WP-CLI requires a SSH access.

List the Available WP-CLI Commands

To list all the commands write "wp help" or just "wp". The following list will be shown:

If you want to get detailed documentation, for example for the "wp db export" command, enter the command listed below:

The result will be as follows:

Update WordPress via WP-CLI

First, you need to check the current version of WordPress that you have:

If you would like to upgrade your WordPress core to a newer version, you can do that via the following command:

Update WordPress Plugins using WP-CLI

If you would like to upgrade any additional plugins or themes on your WordPress website, you could do that with WP-CLI. To check for example the status of the currently installed plugins, use this command:

We have 5 plugins integrated in our sample installation. There is an upgrade for one of them. To perform an upgrade for that plugin, the following command can be used:

Install and Activate WordPress Plugins & Themes via WP-CLI

To install and activate a new plugin, execute the commands listed below:

You should replace the "Plugin_Name" string with the exact plugin name.

Installation and usage of WP Doctor

WP Doctor is a tool developed to help in diagnosing problems with WordPress websites such as failed update, blank page, upload issues, etc. To use the tool, you should first install it. You can do that by running the command below in your WordPress website's document root:

After the tool is successfully installed, you can start using it with the wp doctor command.

To check all available options of the command, use:

To run all default checks, execute the following command:

The checks that the tool will run are:

core-verify-checksums - Check if WordPress verifies against its checksums.

file-eval - Checks files on the filesystem for regex pattern eval\(.*base64_decode\(.* which in many cases indicates suspicious code.

cache-flush - Detects the number of occurrences of the wp_cache_flush() function.

autoload-options-size – Warns when autoloaded options size exceeds the threshold of 900 kb.

constant-savequeries-falsy - Confirms expected state of the SAVEQUERIES constant. SAVEQUERIES causes WordPress to save a backtrace for every SQL query, which is a resource heavy operation, thus using SAVEQUERIES in production is discouraged.

constant-wp-debug-falsy – Verifies if wp debug is enabled by checking the state of the state of the WP_DEBUG constant.

core-update - Verifies if the application is up to date. Errors are shown when new WordPress minor release is available; warns for a major release.

cron-count - Checks the count of cron jobs and displays an error when there is an excess of 50 total cron jobs registered.

cron-duplicates - Checks the count of duplicate cron jobs and displays an error when there is an excess of 10 duplicate cron jobs registered.

option-blog-public - Verifies if the website is publicly available by checking the value of the blog_public option.

plugin-active-count - Shows a warning when there are more than 80 plugins activated.

plugin-deactivated - Warns when more than 40% of plugins are deactivated.

plugin-update - Shows a warning when there are plugin updates available.

theme-update - Warns when there are theme updates available.

php-in-upload - Shows a warning when a PHP file is present in the Uploads folder.

language-update - Checks if there is a new version of the installed language.

If you prefer to run only one or a few of the checks above, you can just specify them instead of using the --all option. You can check how to do that in the two examples below:

or

Installation and usage of WP Profile

WP Profile is a tool developed to help in diagnosing problems with slow WordPress websites. The main idea of the extension is to split the loading process into stages and then give the ability to track each one of them. It provides information about timings, callbacks and locations. To use the tool, you should first install it. You can do that by running the command below in your WordPress website's document root:

After the tool is successfully installed, you can start using it with the wp profile command.

To check all available options of the command, use:

The subcommands of wp profile are:

  • eval - Profile arbitrary code execution. Arbitrary code execution (ACE) is used to describe an attacker's ability to execute arbitrary commands or code on a target website.
  • eval file - Profile execution of an arbitrary file.
  • hook - Profile key metrics for WordPress hooks (actions and filters).
  • stage - Profile each stage of the WordPress load process (bootstrap, main_query, template).

To investigate the slow loading of a website, start with using the stage subcommand as shown below:

You should run that in your website's document root. The output is a table showing information about the loading process of your website. The most important columns of that table are:

  • Stage – The stages are 3 - bootstrap, main_query, template. In a few simple words - bootstrap is where WordPress is setting itself up, loading plugins and the main theme, and firing the init hook , main_query is how WordPress deals with requests and process them in its queries, template is where WordPress determines which theme template to render based on the main query, and renders it.
  • Time – Shows the time that each stage needs to load.
  • Cache ratio – Shows the percentage of caching on the different stages.

Check which of the stages take most time to load (e.g. bootstrap) and run the command below:

Use --spotlight to filter the zero values.

What usually slows down WordPress instances are not the core functionalities, but its plugins. Track them using:

The output of this command will provide you with information about the loading times of different plugins.

Other Tasks you Can do With WP-CLI

You can write posts directly from the command line. To create a post, use the following command:

If you do not need a certain post you can easily delete it:

You can also create a backup of the WordPress database via WP-CLI, by using the following command:

If you decide to change the primary domain for the WordPress web site you can easily update the corresponding URL options:

More details on WP cli can be found through the help command and the project's home page.