DEV Community

Abhishek Deshpande
Abhishek Deshpande

Posted on

Guide to WP-CLI for WordPress Developers

WordPress powers over 43% of websites globally and is a favorite among developers managing multiple sites. To save time and work smarter, try WP-CLI, a tool that helps you manage WordPress from the command line. This guide explains what WP-CLI is, how to use it, and the best commands for faster WordPress maintenance.


What is WP-CLI?

WP-CLI stands for WordPress Command Line Interface. It lets you manage WordPress sites directly from your terminal without logging into the dashboard. With WP-CLI, you can update plugins, optimize databases, and perform bulk tasks quickly.

Imagine managing 10 websites without opening a browser—WP-CLI makes it possible.


Getting Started with WP-CLI

Step 1: Install WP-CLI

Most hosting services include WP-CLI. If not, you can follow the official installation guide. After installation, go to your WordPress root folder using your terminal.

Step 2: Check the Installation

Run this command:

wp --info
Enter fullscreen mode Exit fullscreen mode

If it shows details like your WP-CLI version and PHP setup, you're ready.

Step 3: Run Commands

Make sure you’re in the WordPress root directory (where wp-config.php is). From here, you can start using commands.


What Can WP-CLI Do?

Here are some common tasks WP-CLI makes easier:

  1. Update WordPress Core: Keep WordPress updated without logging in.
  2. Manage Plugins and Themes: Install, update, or remove plugins and themes in seconds.
  3. Database Tasks: Optimize, clean, or export your database easily.
  4. Manage Content: Add, edit, or delete posts and pages.
  5. Fix URLs: Replace old URLs with new ones during migrations or SSL updates.
  6. Backup and Restore: Export your database or create backups in no time.

Why Use WP-CLI for Maintenance?

WP-CLI is all about saving time and effort. Here’s why it’s great for developers:

  • Speed: Commands are much faster than using the WordPress dashboard.
  • Automation: You can schedule and automate repetitive tasks.
  • Bulk Actions: Handle updates and changes across many items at once.
  • Remote Management: Manage websites via SSH from anywhere.
  • Precision: Access advanced tools like database search-and-replace.

For developers working with tight deadlines, WP-CLI is a game-changer.


Top WP-CLI Commands

Here are some essential commands to get you started:

Core Management

wp core version               # Check WordPress version
wp core update                # Update WordPress
wp core verify-checksums      # Check core file integrity
Enter fullscreen mode Exit fullscreen mode

Plugins

wp plugin list                # See all installed plugins
wp plugin update --all        # Update all plugins
Enter fullscreen mode Exit fullscreen mode

Themes

wp theme list                 # List all installed themes
wp theme update --all         # Update all themes
Enter fullscreen mode Exit fullscreen mode

Database

wp db optimize                # Optimize the database
wp db export backup.sql       # Backup the database
Enter fullscreen mode Exit fullscreen mode

Cache

wp cache flush                # Clear all cache
Enter fullscreen mode Exit fullscreen mode

Search and Replace

wp search-replace "http:" "https:" --all-tables
# Change HTTP to HTTPS across the database
Enter fullscreen mode Exit fullscreen mode

Maintenance Mode

wp maintenance-mode activate   # Turn on maintenance mode
wp maintenance-mode deactivate # Turn off maintenance mode
Enter fullscreen mode Exit fullscreen mode

My Daily WP-CLI Workflow

Here’s a simple routine to keep sites running smoothly:

  1. Check Core Version and Files
   wp core version
   wp core verify-checksums
Enter fullscreen mode Exit fullscreen mode
  1. Update Plugins and Themes
   wp plugin update --all
   wp theme update --all
Enter fullscreen mode Exit fullscreen mode
  1. Optimize Database
   wp db optimize
Enter fullscreen mode Exit fullscreen mode
  1. Run Cron Jobs
   wp cron event run --due-now
Enter fullscreen mode Exit fullscreen mode
  1. Clear Cache
   wp cache flush
Enter fullscreen mode Exit fullscreen mode
  1. Fix URLs
   wp search-replace "http:" "https:" --all-tables
Enter fullscreen mode Exit fullscreen mode

WP-CLI Tips: What to Avoid

  • Wrong Directory: Always run commands in the WordPress root folder.
  • No Backups: Back up your site before big changes.
  • Careless Search-Replace: Be cautious—mistakes can break your site.
  • Blind Updates: Test updates on a staging site first.

WP-CLI is a must-have tool for WordPress developers. It makes managing websites easier, faster, and more efficient. Whether you manage one site or many, WP-CLI can save you hours of work.

Try it today to see how it can transform your workflow. If you have questions or tips, share them below!

Top comments (0)