Knowledge Base

How to administer WordPress from the command line

This article describes how to use the WP-CLI (WordPress Command Line Interface) tool to administer WordPress sites, as well as how to use it in cron jobs to automate tasks.

About WP-CLI

It is often quicker and easier to do many tasks at the command line, and WordPress administration is no exception. You can use the WP-CLI (WordPress command-line interface) tool to back up and restore WordPress sites, install plugins, and much more. Additionally, you can use WP-CLI in cron jobs to automate tasks. WP-CLI provides similar functionality to the Drush command-line tool for Drupal.

Because WP-CLI is a command-line tool, it helps if you already have some basic familiarity with the Linux command-line environment. If you have never worked in the Linux command-line environment before, you can learn the basics by reading this article.

Using WP-CLI at the command line

WP-CLI has many commands, and the rest of this article is only an introduction to its capabilities. However, to view WP-CLI's online help at any time, type the following command:

wp help

Alternatively, to view the online help for a specific WP-CLI subcommand, add it to the help command. For example, to view the online help for the wp plugin command, type the following command:

wp help plugin
You should run all of the following commands from the directory where WordPress is installed. Most of these commands will not work correctly if you run them in a directory that does not have a WordPress installation.
Configuring WordPress plugins

WP-CLI makes it easy to work with plugins. For example, to list all of the plugins that are currently installed on your site, type the following command:

wp plugin list

To search for all plugins relating to a particular term, you can use the search option. For example, to search for all plugins related to SEO (search engine optimization), type the following command:

wp plugin search seo

Similarly, installing and activating a new plugin is much quicker than using the web administration interface. To do this, type the following command, replacing name with the name of the plugin that you want to install:

wp plugin install name --activate

To update all of your plugins at once, type the following command:

wp plugin update
By running this command in a cron job, you can ensure that all of your plugins are kept up to date automatically.
Updating WordPress

Updating WordPress is simple with WP-CLI. To do this, type the following command:

wp core update

Whenever you update WordPress, you should also update the database. To do this, type the following command:

wp core update-db

To verify the WordPress version that your site is running, you can type the following command:

wp core version
Backing up and restoring a WordPress database

It is a good idea to periodically back up your WordPress database. By combining WP-CLI's backup functionality with a cron job, you can automatically back up your database on a set schedule.

To back up a WordPress database using WP-CLI, type the following command:

wp db export

When this command finishes, you have a .sql file that you can store in a safe location. If you need to import a database into WordPress at some point, type the following command. Replace filename with the name of the database backup file:

wp db import filename.sql
Be careful! This command completely overwrites the existing WordPress database with the database contained in the backup file.
Optimizing and repairing a WordPress database

In addition to backing up and restoring WordPress databases, you can optimize and repair them. By periodically optimizing databases, you can help ensure that your WordPress sites run efficiently. To do this, type the following command:

wp db optimize

Additionally, if you encounter MySQL errors when running WordPress, you can use WP-CLI to try and repair the database tables. To do this, type the following command:

wp db repair
This command only works on database tables that use the MyISAM storage engine. It does not work on InnoDB tables. For more information about MySQL database storage engines, please see this article.
Backing up and restoring WordPress content

In addition to backing up and restoring databases with the wp db command, you can back up and restore site content. WP-CLI exports site content, including posts, pages, comments, and categories, to a WXR (WordPress eXtended RSS) file.

To back up a WordPress site's content, type the following command:

wp export

When this command finishes, you have a .xml file that you can store in a safe location. If you need to import a WXR file into WordPress at some point, type the following command. Replace filename with the name of the backup file:

wp import filename.xml --authors=create
To import WXR files, you must have the WordPress Importer plugin installed and activated.
Managing WordPress options

WP-CLI provides an easy way to manage WordPress options. For example, to change the admin e-mail address, type the following command, replacing [email protected] with the e-mail address you want to use:

wp option update admin_email [email protected]

If you have set a custom login URL and need to retrieve this information, type the following command:

wp option get rwl_page

Using WP-CLI in a cron job

By combining WP-CLI's functionality with cron jobs, you can easily and automatically do administrative tasks that from the WordPress administration interface are time consuming and repetitive.

 

For example, the following cron line demonstrates how to back up a WordPress site every day at 2:15 AM using WP-CLI:

15 2 * * * cd /home/username/public_html; /usr/bin/wp export >/dev/null 2>&1

Similarly, the following cron line demonstrates how to automatically check for and install any available WordPress updates every day at 3:15 AM:

15 3 * * * cd /home/username/public_html; /usr/bin/wp core update >/dev/null 2>&1

More Information

For more information about WP-CLI, please visit http://wp-cli.org.