WP-CLI Tutorial: Simplify WordPress Management with Essential Commands

WP-CLI Tutorial: Simplify WordPress Management with Essential Commands
WP-CLI Tutorial: Simplify WordPress Management with Essential Commands

Simplify WordPress management with WP-CLI. Learn essential commands, advanced tasks, and troubleshooting tips to master the command-line interface.

The first time you have to update the same plugin on twelve sites, the dashboard stops feeling convenient. Click in, log in, find the plugin, click update, wait, repeat. WP-CLI is the fix. It’s the command-line interface for WordPress, and it lets you do from the terminal what you’d normally click through in wp-admin: install plugins, update core, export a database, run a search and replace. One command instead of ten screens.

This guide walks through installing WP-CLI, the commands you’ll reach for daily, and a few advanced tasks worth knowing. If you’re new to it, start at the top. If you’ve used it before, skip to the commands.

Table of Contents

What is WP-CLI?

WP-CLI is an open-source tool for managing WordPress from the terminal instead of the browser. It runs on the server, next to your install, so it skips the round trips a dashboard makes. Anything from a quick “what version am I on” to a full database search-and-replace across a migrated domain runs as a single command. That’s the whole appeal: less clicking, and steps you can script and repeat.

Installing WP-CLI

You install WP-CLI on the server or local environment where WordPress lives. Here’s the process.

1. Check System Requirements

Before you install, make sure the environment has:

  • PHP version 7.4 or higher
  • WordPress 3.7 or higher
  • A UNIX-like operating system (Linux, macOS, or compatible)
2. Install WP-CLI

Download the Phar file, check that it runs, then move it somewhere on your PATH so you can call it as wp:

Example of the code
Bash
# Download the WP-CLI Phar file
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# Verify the Phar file
php wp-cli.phar --info
# Make it executable
chmod +x wp-cli.phar
# Move it to a global location
sudo mv wp-cli.phar /usr/local/bin/wp
# Confirm installation
wp --info

Run wp --info and you should see the WP-CLI version, the PHP version, and details about your server. If that prints, you’re installed.

Getting Started with WP-CLI

Change into your WordPress directory first. WP-CLI reads the install from the current folder, so most commands only work from inside the site’s root. Once you’re there, try these:

  • wp core version: Displays the current WordPress version.
  • wp plugin list: Lists all installed plugins and their statuses.
  • wp theme list: Lists all installed themes and their statuses.
  • wp user list: Displays a list of registered users.

If those return real data, WP-CLI is talking to your site correctly.

Essential WP-CLI Commands

These are the commands you’ll use most. Group them in your head by what they touch: core, plugins and themes, and the database.

1. Core Management

For WordPress core files and updates:

  • wp core update: Updates WordPress to the latest version.
  • wp core verify-checksums: Verifies core files against the official checksums, so you know nothing’s been tampered with.
  • wp core install --url="example.com" --title="My Site" --admin_user="admin" --admin_password="password" --admin_email="[email protected]": Installs WordPress from the command line.
2. Plugin and Theme Management

For everything you’d otherwise do on the Plugins and Themes screens:

  • wp plugin install plugin-slug --activate: Installs and activates a plugin in one step.
  • wp plugin update --all: Updates every installed plugin at once.
  • wp theme install theme-slug --activate: Installs and activates a theme.
3. Database Management

For backups, restores, and cleanup:

  • wp db export: Exports the database as an SQL file.
  • wp db import file.sql: Imports a database backup.
  • wp db optimize: Optimizes the database tables.
  • wp search-replace 'old-url' 'new-url' --dry-run: Previews a site-wide URL swap. Drop --dry-run to run it for real. Note this is search-replace, a top-level command, not part of wp db. It handles serialized data safely, which a raw SQL find-and-replace does not.

Advanced WP-CLI Tasks

Once the basics are second nature, WP-CLI handles bigger jobs too.

1. Multisite Management

For a multisite network:

  • wp site list: Lists all sites in the network.
  • wp site create --slug=subsite: Creates a new subsite.
2. Custom Scripts

You can register your own commands. Here’s a small one that clears transients from the options table:

Example of the code
PHP
<?php
// Custom WP-CLI command to clear transients
WP_CLI::add_command('clear_transients', function() {
    global $wpdb;
    $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'");
    WP_CLI::success("Transients cleared!");
});
?>

Troubleshooting with WP-CLI

When a site misbehaves, WP-CLI is often faster than digging through wp-admin, especially if the admin itself is broken:

  • wp config set WP_DEBUG true --raw: Turns on WordPress debug mode by writing the WP_DEBUG constant into wp-config.php. The --raw flag writes true as a boolean, not a string.
  • wp db check: Checks the database for errors.
  • wp plugin deactivate plugin-slug --skip-plugins: Deactivates a plugin while skipping all plugins to load, handy when a plugin is fataling the site.
  • wp cache flush: Flushes the object cache when stale data is the culprit.

Best Practices for Using WP-CLI

A few habits that keep WP-CLI from biting you:

  • Back up before changes: Run wp db export before anything destructive. A search-replace or an update gone wrong is much less scary with a fresh dump on disk.
  • Use SSH for remote sites: Run commands over an SSH connection rather than opening the database to the outside.
  • Keep WP-CLI updated: Update it now and then so command syntax and features stay current with WordPress.
Conclusion

WP-CLI won’t change what WordPress does. It changes how fast you get there. Updates, backups, migrations, and troubleshooting all collapse into a line or two of typing, and once a task is a command, it’s a command you can script and repeat.

Start with the handful above: wp core version, wp plugin update --all, wp db export. Get comfortable, then reach for search-replace and your own custom commands when you need them. The time you save adds up fast.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top