How to – WordPress – command line – WP-CLI

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.

Installing WP-CLI

WP-CLI is not included with the default WordPress installation, so you must download and install it. To do this, follow these steps:

  1. Log in to your account using SSH.
  2. To download WP-CLI, type the following commands:
    cd ~
    curl -k -L https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > wp-cli.phar
  3. To test that the WP-CLI file works correctly, type the following command:
    php wp-cli.phar --info

    WP-CLI displays PHP version and configuration information.

    You may receive the following error message:

    Fatal error: Class 'Phar' not found in /home/username/wp-cli.phar on line 3

    This message means the Phar (“PHP Archive”) extension is not loaded by default on your account. If you receive this message, type the following command instead to load the Phar extension:

    php -d extension=phar.so wp-cli.phar --info
  4. Type one of the following commands, depending on your server configuration:
    • If you did not receive an error message in step 3, type the following command. Replace USERNAME with your account username:
      echo 'alias wp="php /home/USERNAME/wp-cli.phar"' >> ~/.bashrc
    • If you received an error message in step 3, type the following command instead. Replace USERNAME with your account username:
      echo 'alias wp="php -d extension=phar.so /home/USERNAME/wp-cli.phar"' >> ~/.bashrc
  5. To make the setting take effect immediately, type the following command:
    source ~/.bashrc
  6. You can now run WP-CLI by simply typing wp.

Using WP-CLI at the command line

After you install WP-CLI, you are ready to use it to administer WordPress sites. 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.

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.

 

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.

Cron does not use the settings in your .bashrc file, however, so the wp alias you defined above in the Installing WP-CLI procedure will not work for cron jobs. Instead, you must specify absolute paths to the PHP executable and to the wp-cli.phar file. Additionally, you must change the working directory to the WordPress installation.

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/local/bin/php /home/username/wp-cli.phar export >/dev/null 2>&1

If the Phar extension is not loaded by default on your account, you would use the following line instead:

15 2 * * * cd /home/username/public_html; /usr/local/bin/php -d extension=phar.so /home/username/wp-cli.phar 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/local/bin/php /home/username/wp-cli.phar core update >/dev/null 2>&1

How to administer WordPress from the command line

More Information

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

Scroll to top