Image by Simon from Pixabay

Image by <a href="https://pixabay.com/users/simon-3/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=886462">Simon</a> from <a href="https://pixabay.com//?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=886462">Pixabay</a>

 

 

🧠 What Is WP-CLI?

WP-CLI is the command-line interface for WordPress. It allows developers, sysadmins, and power users to manage WordPress installations without touching the browser. From installing plugins to managing users, updating core files, and even running database queries—WP-CLI is a powerhouse for automation and efficiency.

Think of it as wp-admin on steroids, but scriptable, faster, and ideal for bulk operations or headless environments.

🚀 Why Use WP-CLI?

  • Speed: No need to wait for page loads or click through menus.
  • Automation: Perfect for cron jobs, CI/CD pipelines, and provisioning scripts.
  • Remote Management: SSH into a server and run commands instantly.
  • Bulk Operations: Update hundreds of posts, users, or plugins in seconds.
  • Debugging: Access logs, check memory usage, and inspect the database directly.

🛠️ Installation

✅ Prerequisites

  • PHP 5.6+ (PHP 7.4+ recommended)
  • UNIX-like environment (Linux, macOS, WSL)
  • WordPress installed

📦 Install WP-CLI Globally

bash
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

Now you can run wp from anywhere.

📂 Directory Context

WP-CLI must be run from the root of a WordPress installation (where wp-config.php lives), unless you specify --path.

bash
cd /var/www/html/my-site
wp plugin list

Or use:

bash
wp --path=/var/www/html/my-site plugin list

📋 Common WP-CLI Commands

🔧 Core Management

bash
wp core download
wp core install --url="example.com" --title="My Site" --admin_user="admin" --admin_password="pass" --admin_email="[email protected]"
wp core update

🧩 Plugin Management

bash
wp plugin install jetpack --activate
wp plugin deactivate akismet
wp plugin update --all
wp plugin delete hello

🎨 Theme Management

bash
wp theme install twentytwentythree --activate
wp theme list
wp theme delete twentytwenty

👤 User Management

bash
wp user create john [email protected] --role=editor
wp user list
wp user delete 5 --reassign=1

📝 Post Management

bash
wp post create --post_type=page --post_title='About Us' --post_status=publish
wp post list --post_type=post --format=table
wp post delete 123

🧠 Database Operations

bash
wp db export backup.sql
wp db import backup.sql
wp db optimize
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type='product';"

🔍 Search & Replace

bash
wp search-replace 'http://oldsite.com' 'https://newsite.com'

Supports dry runs and serialization-safe replacements.

🧪 Advanced Usage

🧵 Cron Jobs

bash
wp cron event list
wp cron event run --due-now

🧼 Cache Clearing

bash
wp cache flush

🔐 Security & ACLs

bash
wp user update 2 --role=administrator
wp user meta update 2 wp_capabilities '{"administrator":1}'

📦 Multisite Support

bash
wp site list
wp site create --slug=subsite --title="Sub Site" [email protected]

Use --url=subsite.example.com for subdomain installs.

🧰 Scripting WP-CLI

You can wrap WP-CLI commands in bash scripts for automation:

bash
#!/bin/bash
wp plugin update --all
wp theme update --all
wp db export "backup-$(date +%F).sql"

Or use it in CI/CD pipelines with GitHub Actions, GitLab CI, or Jenkins.

 

🧩 WP-CLI Packages

Extend WP-CLI with packages:

bash
wp package install wp-cli/profile-command
wp profile stage --all

Or build your own with custom commands using PHP classes.

🧠 Final Tips

  • Use --debug to troubleshoot
  • Use --format=json for API integrations
  • Use wp shell for interactive PHP debugging
  • Always back up before running destructive commands

📚 More Resources

By staff