
🧠 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
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.
cd /var/www/html/my-site
wp plugin list
Or use:
wp --path=/var/www/html/my-site plugin list
📋 Common WP-CLI Commands
🔧 Core Management
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
wp plugin install jetpack --activate
wp plugin deactivate akismet
wp plugin update --all
wp plugin delete hello
🎨 Theme Management
wp theme install twentytwentythree --activate
wp theme list
wp theme delete twentytwenty
👤 User Management
wp user create john [email protected] --role=editor
wp user list
wp user delete 5 --reassign=1
📝 Post Management
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
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
wp search-replace 'http://oldsite.com' 'https://newsite.com'
Supports dry runs and serialization-safe replacements.
🧪 Advanced Usage
🧵 Cron Jobs
wp cron event list
wp cron event run --due-now
🧼 Cache Clearing
wp cache flush
🔐 Security & ACLs
wp user update 2 --role=administrator
wp user meta update 2 wp_capabilities '{"administrator":1}'
📦 Multisite Support
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:
#!/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:
wp package install wp-cli/profile-command
wp profile stage --all
Or build your own with custom commands using PHP classes.
🧠 Final Tips
- Use
--debugto troubleshoot - Use
--format=jsonfor API integrations - Use
wp shellfor interactive PHP debugging - Always back up before running destructive commands
