Skip to content

How to Use WP-CLI to Manage WordPress from the Command Line

Published:  at  đź•“ 02:00 PM
⏰ 8 min read

Managing a WordPress site through the admin dashboard can feel like wading through molasses, especially when you’re juggling multiple sites or performing repetitive tasks. As a WordPress developer who’s spent years optimizing workflows for clients, I’ve found WP-CLI to be a game-changer. This command-line tool lets you control virtually every aspect of WordPress—updates, backups, user management, and more—with a few keystrokes.

In this comprehensive guide, I’ll show you how to harness WP-CLI to streamline your WordPress management. Whether you’re a developer automating client sites, a blogger scaling content, or a marketer tweaking settings, this tutorial will empower you to work faster and smarter. Let’s dive into the world of command-line WordPress management!


Table of Contents

Open Table of Contents

WP-CLI Overview

WP-CLI is an open-source command-line interface for managing WordPress sites. It allows you to perform tasks like installing plugins, updating themes, managing users, and running database queries without touching the WordPress dashboard.

Why Use WP-CLI?

The WordPress admin dashboard is user-friendly but slow for bulk tasks or server-side work. WP-CLI offers:

  • Speed: Execute tasks in seconds.
  • Automation: Script repetitive tasks (e.g., updates, backups).
  • Precision: Avoid human error from manual clicks.
  • Scalability: Manage multiple sites efficiently.

When Should You Use WP-CLI?

Use WP-CLI when:

  • Managing multiple WordPress sites.
  • Automating tasks like updates or backups.
  • Performing complex operations (e.g., database migrations).
  • Working on a server without a graphical interface.

Who Should Use WP-CLI?

  • Developers: Automate workflows and manage client sites.
  • Bloggers: Quickly update plugins or export content.
  • Marketers: Analyze data or manage users without the dashboard.
  • Sysadmins: Perform server-side maintenance.

Where Does WP-CLI Run?

WP-CLI runs on your server or local machine via a terminal (e.g., SSH for remote servers or a local terminal for development environments). It requires access to your WordPress installation’s root directory.

How Does WP-CLI Work?

WP-CLI interacts with WordPress’s core functions through PHP commands. You run commands like wp plugin install or wp db export in a terminal, and WP-CLI executes them directly on your WordPress installation.


Step-by-Step Guide to Using WP-CLI

Here’s how to set up and use WP-CLI to manage your WordPress site. I’ll include practical examples to get you started.

Step 1: Install WP-CLI

WP-CLI requires PHP (5.6 or higher) and a command-line environment. Most hosting providers (e.g., SiteGround, DigitalOcean) support it.

  1. Check System Requirements:

    • Ensure PHP and cURL are installed:
      php -v
      curl -V
    • You’ll need SSH access for remote servers or a local terminal (e.g., Terminal on macOS, Command Prompt on Windows).
  2. Download and Install WP-CLI:

    • On Linux/macOS, run:
      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
    • On Windows, download wp-cli.phar from wp-cli.org and move it to a folder (e.g., C:\wp-cli). Add it to your system PATH.
  3. Verify Installation:

    • Run:
      wp --info
    • You should see details about your PHP version and WP-CLI configuration.

Step 2: Navigate to Your WordPress Directory

  • Use SSH to access your server or open a terminal in your local WordPress root directory (where wp-config.php is located).
  • Example:
    cd /var/www/html/your-site

Step 3: Run Basic WP-CLI Commands

Here are some common commands to get you started:

  1. Check WordPress Version:

    wp core version
  2. Update WordPress Core:

    wp core update
  3. List Installed Plugins:

    wp plugin list
  4. Install a Plugin:

    wp plugin install yoast-seo --activate
  5. Export Database:

    wp db export backup.sql

Step 4: Automate Tasks with WP-CLI

WP-CLI shines for automation. You can create scripts or cron jobs to handle repetitive tasks.

Example: Automate Plugin Updates

  • Create a bash script (update-plugins.sh):
    #!/bin/bash
    cd /var/www/html/your-site
    wp plugin update --all
    wp core update
    wp theme update --all
  • Make it executable:
    chmod +x update-plugins.sh
  • Schedule it with a cron job:
    crontab -e
    0 2 * * * /path/to/update-plugins.sh
    (Runs daily at 2 AM.)

Example: Bulk User Management

  • Add a new admin user:

    wp user create newuser [email protected] --role=administrator --user_pass=securepassword
  • Delete inactive users:

    wp user delete $(wp user list --field=ID --role=subscriber) --yes

Step 5: Test and Troubleshoot

  • Check Command Output: Always review command outputs for errors (e.g., Error: Plugin not found).
  • Use —debug: Add --debug to troubleshoot issues:
    wp plugin install yoast-seo --debug
  • Verify Permissions: Ensure your server user has write access to the WordPress directory.

Real-World Use Cases

WP-CLI isn’t just for routine updates—it’s a versatile tool that can solve real-world problems and streamline complex workflows. Below are some practical scenarios where WP-CLI shines, demonstrating its power to save time, reduce errors, and simplify WordPress management for developers and site owners alike.

  1. Managing Multiple Sites: A client with 10 WordPress sites used WP-CLI to update plugins across all sites with a single script, saving hours of manual work.
  2. Database Migrations: I used wp db export and wp db import to migrate a WooCommerce store to a new host without downtime.
  3. Content Cleanup: A blogger removed 500 spam comments in seconds with wp comment delete $(wp comment list --status=spam --field=ID) --yes.
  4. Performance Testing: A developer used wp transient delete --all to clear transients and optimize site speed.
  5. Bulk User Management: An agency onboarded 50 new users at once using wp user create in a loop, automating account setup for a membership site.
  6. Automated Backups: A sysadmin scheduled nightly database and file backups with WP-CLI and cron, ensuring regular restore points without manual intervention.
  7. Theme and Plugin Audits: A security consultant quickly listed outdated plugins and themes across multiple sites using wp plugin list and wp theme list, streamlining vulnerability checks.

Pros and Cons of WP-CLI

WP-CLI offers significant advantages for managing WordPress, but it’s not without its challenges. Understanding the strengths and limitations of WP-CLI will help you decide when and how to use it most effectively. Here’s a quick overview of the main pros and cons to consider:

ProsCons
Rapidly updates core, plugins, and themes with single commandsRequires familiarity with the command line
Enables powerful automation via scripts and cron jobsMistyped commands can cause critical site issues
Supports multisite and bulk operations across many sitesLimited to environments with PHP and SSH access
Reduces manual errors compared to dashboard workflowsSome hosts may restrict WP-CLI usage
Easily integrates with CI/CD and deployment pipelinesLacks a graphical interface—steeper learning curve

Common Mistakes to Avoid

It’s important to be aware of common pitfalls that can trip up even experienced users. Avoiding these mistakes will help you use WP-CLI safely and efficiently, ensuring your WordPress site remains stable and secure. Here are some key issues to watch out for:

  1. Running Commands as Root: Use a non-root user to avoid permission issues.

  2. Not Backing Up: Always back up your site before running destructive commands (e.g., wp db reset).

  3. Ignoring Errors: Read command outputs carefully to catch issues early.

  4. Incorrect Directory: Ensure you’re in the WordPress root directory before running commands.

  5. Overwriting Files: Be cautious with commands like wp db import that may overwrite data.


Comparison with Alternatives

Before diving into the comparison, it’s important to understand how WP-CLI stacks up against other WordPress management tools. Each method—WP-CLI, the WordPress dashboard, and third-party plugins—offers unique advantages and limitations. Here’s a quick overview to help you choose the best tool for your workflow.

FeatureWP-CLIWordPress DashboardPlugins (e.g., ManageWP)
SpeedVery fastSlow for bulk tasksModerate
Ease of UseRequires technical skillsBeginner-friendlyUser-friendly
AutomationExcellent (scripts, cron)LimitedGood (paid plans)
CostFreeFreeFree or paid
FlexibilityHighly customizableLimited to GUIPlugin-dependent

Alternative: Tools like ManageWP or InfiniteWP offer GUI-based management for multiple sites but lack the precision and speed of WP-CLI. The dashboard is great for beginners but slow for developers.


Benefits and Key Takeaways

Key Takeaway: WP-CLI is a must-have tool for developers and advanced WordPress users. It transforms tedious tasks into quick, automated processes, saving time and reducing errors. Other benefits include:

  • Efficiency: WP-CLI cuts task time from minutes to seconds.
  • Automation: Scripts and cron jobs streamline maintenance.
  • Scalability: Manage one or hundreds of sites with ease.
  • Control: Execute precise commands without navigating menus.
  • Community Support: A large community means plenty of resources and plugins.
  • Integration: Works well with CI/CD pipelines for deployment.

WP-CLI is a powerhouse for managing WordPress sites from the command line. From installing plugins to automating updates, it offers unmatched speed and flexibility for developers and power users. By following the steps above—installing WP-CLI, running basic commands, and automating tasks—you can take control of your WordPress site like never before.

As someone who’s used WP-CLI to manage client sites and personal projects, I can vouch for its ability to save time and reduce stress. Whether you’re maintaining one blog or a network of sites, WP-CLI is worth learning.


Further Reading


FAQ

  • Do I need coding skills to use WP-CLI?
    Basic command-line knowledge helps, but WP-CLI’s commands are straightforward. Start with simple tasks like wp plugin list.

  • Can I use WP-CLI on shared hosting?
    Yes, if your host provides SSH access. Check with your provider (e.g., SiteGround, Bluehost).

  • Is WP-CLI safe for production sites?
    Yes, but always back up your site before running commands, especially destructive ones like wp db reset.

  • Can WP-CLI manage multiple sites?
    Yes, run WP-CLI in each site’s root directory or use the --path flag (e.g., wp plugin update --all --path=/var/www/site2).

  • How do I troubleshoot WP-CLI errors?
    Use the --debug flag, check file permissions, and ensure you’re in the correct directory.