How to Use WP-CLI
Manage WordPress from the command line with WP-CLI. Install plugins, update core, manage users, and run database operations without touching the admin dashboard.
4 min read
WP-CLI is a command-line tool for managing WordPress installations. Instead of clicking through the admin dashboard, you type commands in your terminal. It’s quicker for repetitive jobs and easy to script.
Is WP-CLI Available?
WP-CLI needs shell (SSH) access:
- Shared and WordPress hosting: Not available. These plans have no SSH, SFTP or command line. Use the WordPress dashboard, cPanel’s File Manager, phpMyAdmin, and Softaculous instead, or open a support ticket for jobs like a search-replace.
- VPS and AMD EPYC VPS (Business Cloud): Available over SSH. On a self-managed server you install it yourself (below).
- Enhance: Available over SSH to your server
Test it:
wp --version
If it’s not installed on your VPS:
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
Basic Usage
Always run WP-CLI from your WordPress installation directory:
cd /var/www/yourdomain.com
# or, on a VPS with cPanel, as the account user:
cd /home/username/public_html
Essential Commands
Core Management
# Check WordPress version
wp core version
# Update WordPress core
wp core update
# Update the database after a core update
wp core update-db
# Download WordPress (fresh install)
wp core download
# Install WordPress
wp core install --url="yourdomain.com" --title="My Site" --admin_user="yourname" --admin_email="[email protected]" --prompt=admin_password
Plugin Management
# List all plugins
wp plugin list
# Install a plugin
wp plugin install litespeed-cache --activate
# Update a specific plugin
wp plugin update contact-form-7
# Update all plugins
wp plugin update --all
# Deactivate a plugin
wp plugin deactivate wordfence
# Delete a plugin
wp plugin delete hello-dolly
# Search for plugins
wp plugin search "seo" --per-page=5
Theme Management
# List themes
wp theme list
# Install and activate a theme
wp theme install generatepress --activate
# Update all themes
wp theme update --all
User Management
# List users
wp user list
# Create a user
wp user create newadmin [email protected] --role=administrator --user_pass="strongpassword"
# Reset a password
wp user update 1 --user_pass="newpassword"
# Delete a user (reassign their posts to user ID 1)
wp user delete 5 --reassign=1
Database Operations
# Optimize database tables
wp db optimize
# Export the database
wp db export backup.sql
# Import a database
wp db import backup.sql
# Run a SQL query
wp db query "SELECT option_value FROM wp_options WHERE option_name = 'siteurl'"
# Search and replace (useful for migrations)
wp search-replace 'http://oldsite.com' 'https://newsite.com' --dry-run
wp search-replace 'http://oldsite.com' 'https://newsite.com'
Content Management
# Delete all spam comments
wp comment delete $(wp comment list --status=spam --format=ids)
# Delete all post revisions
wp post delete $(wp post list --post_type=revision --format=ids)
# Generate test posts (for development)
wp post generate --count=10
# Delete transients
wp transient delete --all
Options and Settings
# Get the site URL
wp option get siteurl
# Update the site URL
wp option update siteurl "https://yourdomain.com"
wp option update home "https://yourdomain.com"
# Get the total size of autoloaded options (performance check)
wp db query "SELECT SUM(LENGTH(option_value)) FROM $(wp db prefix)options WHERE autoload IN ('yes','on','auto-on','auto');"
Maintenance
# Clear the object cache
wp cache flush
# Regenerate thumbnails
wp media regenerate --yes
# Run cron events
wp cron event run --due-now
# Check site health (doctor is a separate package:
# install it once with: wp package install wp-cli/doctor-command)
wp doctor check --all
Practical Recipes
Quick WordPress Install
cd /var/www/yourdomain.com
wp core download
wp config create --dbname=wp_database --dbuser=wp_user --dbpass=password
wp core install --url="yourdomain.com" --title="My Site" --admin_user=yourname [email protected] --prompt=admin_password
wp plugin install litespeed-cache wordfence --activate
wp rewrite structure '/%postname%/'
Fix a Hacked Site
Add --skip-plugins --skip-themes to every command on a compromised site. Without it, WP-CLI loads WordPress with the infected plugins and theme and runs their code.
# 1. Check which core files were modified or added (do this first, before replacing anything)
wp core verify-checksums --skip-plugins --skip-themes
# 2. Reinstall the same core version you run now, without touching wp-content
wp core download --force --skip-content --version=$(wp core version --skip-plugins --skip-themes) --skip-plugins --skip-themes
# 3. Reinstall all plugins from WordPress.org (premium plugins must be re-uploaded from the vendor)
wp plugin install $(wp plugin list --field=name --skip-plugins --skip-themes) --force --skip-plugins --skip-themes
# 4. Reset each administrator's password (you'll be prompted for the new one)
wp user update $(wp user list --role=administrator --format=ids --skip-plugins --skip-themes) --prompt=user_pass --skip-plugins --skip-themes
verify-checksums only checks core files. Extra files it reports inside wp-admin or wp-includes should be deleted, since core download doesn’t remove them.
Speed Audit
# Check autoloaded data size (should be under 800KB)
wp db query "SELECT SUM(LENGTH(option_value)) as total_bytes FROM $(wp db prefix)options WHERE autoload IN ('yes','on','auto-on','auto');"
# Find the largest autoloaded options
wp db query "SELECT option_name, LENGTH(option_value) as size FROM $(wp db prefix)options WHERE autoload IN ('yes','on','auto-on','auto') ORDER BY size DESC LIMIT 20;"
# Delete transients
wp transient delete --all
# Clean revisions
wp post delete $(wp post list --post_type=revision --format=ids) 2>/dev/null
Related Articles
Last updated 21 Oct 2026, 00:00 +0300.