| name | wp-cli-workflows |
| description | Use when scaffolding WordPress themes or plugins, performing database operations, managing WordPress core/plugins/themes, creating users, or automating WordPress tasks with WP-CLI. Keywords: WP-CLI, scaffold, database, export, import, search-replace, multisite, WordPress automation, wp command |
WP-CLI Workflows
Overview
WP-CLI is the command-line interface for WordPress, enabling rapid scaffolding, database operations, and WordPress management without using the admin dashboard. Proper WP-CLI usage requires understanding command structure, using flags correctly, and following safety practices (backups, dry-runs).
Core Principle: ALWAYS use --dry-run for destructive operations, ALWAYS backup before database changes, ALWAYS verify command syntax before execution.
When to Use
Use this skill when:
- Scaffolding new themes or plugins
- Performing database backup/restore/migration
- Updating WordPress core, plugins, or themes
- Creating or managing users
- Running search-replace operations
- Managing multisite installations
- Automating WordPress tasks
- Debugging WordPress issues
Symptoms that trigger this skill:
- "scaffold theme"
- "create plugin"
- "export database"
- "import database"
- "search-replace"
- "update WordPress"
- "create user"
- "wp command"
When NOT to use:
- GUI-based admin tasks (user preference for visual interface)
- Simple content creation (better done in admin)
- Tasks requiring visual confirmation
Quick Reference
Essential WP-CLI Commands
| Task | Command | Flags to Know |
|---|
| Download WordPress | wp core download | --version, --locale |
| Create wp-config | wp config create | --dbname, --dbuser, --dbpass |
| Install WordPress | wp core install | --url, --title, --admin_user |
| Update WordPress | wp core update | --version, --force |
| Scaffold theme | wp scaffold _s | --theme_name, --author |
| Scaffold plugin | wp scaffold plugin | --plugin_name, --plugin_description |
| Activate theme | wp theme activate | None |
| Install plugin | wp plugin install | --activate, --version |
| Export database | wp db export | --add-drop-table |
| Import database | wp db import | None |
| Search-replace | wp search-replace | --dry-run, --all-tables |
| Create user | wp user create | --role, --user_pass |
| Flush cache | wp cache flush | None |
| Rewrite flush | wp rewrite flush | None |
Safety Flags (ALWAYS use these)
| Flag | Purpose | When to Use |
|---|
--dry-run | Preview changes without executing | search-replace, destructive operations |
--yes | Skip confirmation prompts | Automation scripts |
--skip-plugins | Run without active plugins | Debugging plugin conflicts |
--skip-themes | Run without active theme | Debugging theme conflicts |
--allow-root | Run as root user | Server environments (use cautiously) |
Implementation Patterns
Pattern 1: Fresh WordPress Installation
#!/bin/bash
wp core download --locale=en_US
wp config create \
--dbname=mydb \
--dbuser=myuser \
--dbpass=mypassword \
--dbhost=localhost \
--dbprefix=wp_
wp core install \
--url=https://example.com \
--title="My WordPress Site" \
--admin_user=admin \
--admin_password=secure_password_here \
--admin_email=admin@example.com
wp rewrite structure '/%postname%/' --hard
wp rewrite flush
wp post delete 1 --force
wp post delete 2 --force
wp comment delete 1 --force
wp plugin install wordpress-seo --activate
wp plugin install wordfence --activate
echo "WordPress installed successfully!"
Pattern 2: Theme Scaffolding and Development
#!/bin/bash
cd wp-content/themes/
wp scaffold _s mytheme \
--theme_name="My Theme" \
--author="Your Name" \
--author_uri="https://example.com" \
--sassify \
--activate
wp theme list
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw
echo "Theme scaffolded and activated!"
Pattern 3: Plugin Scaffolding
#!/bin/bash
cd wp-content/plugins/
wp scaffold plugin myplugin \
--plugin_name="My Plugin" \
--plugin_description="A custom WordPress plugin" \
--plugin_author="Your Name" \
--plugin_author_uri="https://example.com" \
--plugin_uri="https://example.com/myplugin" \
--activate
cd myplugin
wp scaffold plugin-tests .
bash bin/install-wp-tests.sh wordpress_test root '' localhost latest
wp plugin list
echo "Plugin scaffolded with test suite!"
Pattern 4: Database Backup and Restore
#!/bin/bash
BACKUP_FILE="backup-$(date +%Y%m%d-%H%M%S).sql"
wp db export "$BACKUP_FILE" --add-drop-table
echo "Database backed up to: $BACKUP_FILE"
if [ -f "$BACKUP_FILE" ]; then
echo "Backup verified: $(ls -lh $BACKUP_FILE)"
else
echo "ERROR: Backup failed!"
exit 1
fi
echo "Backup complete. To restore, uncomment restore commands."
Pattern 5: Search-Replace for Domain Migration
#!/bin/bash
OLD_DOMAIN="http://localhost:8000"
NEW_DOMAIN="https://example.com"
BACKUP_FILE="pre-migration-$(date +%Y%m%d-%H%M%S).sql"
wp db export "$BACKUP_FILE"
echo "Backup created: $BACKUP_FILE"
echo "Running dry-run to preview changes..."
wp search-replace "$OLD_DOMAIN" "$NEW_DOMAIN" --dry-run --all-tables
read -p "Proceed with actual search-replace? (yes/no): " CONFIRM
if [ "$CONFIRM" = "yes" ]; then
wp search-replace "$OLD_DOMAIN" "$NEW_DOMAIN" --all-tables --skip-columns=guid
wp cache flush
wp rewrite flush
echo "Migration complete!"
else
echo "Migration cancelled. Backup preserved: $BACKUP_FILE"
fi
Pattern 6: User Management
#!/bin/bash
wp user create johndoe john@example.com \
--role=administrator \
--user_pass=secure_password \
--first_name=John \
--last_name=Doe \
--display_name="John Doe"
wp user create janeeditor jane@example.com \
--role=editor \
--user_pass=secure_password \
--send-email
wp user list --format=table
wp user set-role johndoe editor
wp user delete spam_user --reassign=1
echo "User management complete!"
Pattern 7: Plugin and Theme Updates
#!/bin/bash
wp db export "pre-update-$(date +%Y%m%d).sql"
echo "Checking for updates..."
wp core check-update
wp plugin list --update=available
wp theme list --update=available
wp core update
wp core update-db
wp plugin update --all
wp theme update --all
wp core verify-checksums
wp plugin verify-checksums --all
wp cache flush
echo "Updates complete! Database backup: pre-update-$(date +%Y%m%d).sql"
Pattern 8: Multisite Management
#!/bin/bash
wp core multisite-convert --title="My Network"
wp site create \
--slug=subsite \
--title="My Subsite" \
--email=admin@example.com
wp site list
wp plugin activate akismet --network
wp theme install twentytwentyfour --activate
wp theme enable twentytwentyfour --network
wp --url=subsite.example.com post list
wp site list --field=url | xargs -I {} wp --url={} cache flush
echo "Multisite management complete!"
Pattern 9: Debugging and Diagnostics
#!/bin/bash
wp core version --extra
wp core verify-checksums
echo "Testing without plugins..."
wp plugin deactivate --all
wp plugin activate --all
wp media regenerate --yes
wp db check
wp db optimize
echo "=== WordPress Version ===" > debug-info.txt
wp core version >> debug-info.txt
echo "=== Active Plugins ===" >> debug-info.txt
wp plugin list --status=active >> debug-info.txt
echo "=== Active Theme ===" >> debug-info.txt
wp theme list --status=active >> debug-info.txt
echo "=== PHP Version ===" >> debug-info.txt
php -v >> debug-info.txt
echo "Debug info saved to: debug-info.txt"
Pattern 10: Post and Content Management
#!/bin/bash
wp post generate --count=10 --post_type=post --post_status=publish
wp post list --post_status=publish --format=table
wp post update 123 --post_status=draft
wp post delete $(wp post list --post_status=trash --format=ids) --force
wp export --dir=./export --post_type=post
wp import export.xml --authors=create
wp post list --s="search term" --post_status=any
echo "Content management complete!"
Common Mistakes
1. Running search-replace Without Backup
WRONG:
wp search-replace 'old.com' 'new.com' --all-tables
WHY THIS FAILS:
- No backup if something goes wrong
- Cannot undo changes
- Risk of data corruption
CORRECT:
wp db export backup.sql
wp search-replace 'old.com' 'new.com' --all-tables --dry-run
wp search-replace 'old.com' 'new.com' --all-tables
2. Not Using --dry-run Flag
WRONG:
wp search-replace 'http://' 'https://' --all-tables
WHY THIS FAILS:
- Immediately modifies database
- No preview of what will change
- Cannot verify before execution
CORRECT:
wp search-replace 'http://' 'https://' --all-tables --dry-run
wp search-replace 'http://' 'https://' --all-tables
3. Missing --skip-columns=guid Flag
WRONG:
wp search-replace 'old.com' 'new.com'
WHY THIS FAILS:
- Changes post GUIDs (should remain unchanged)
- Breaks RSS feed readers
- Violates WordPress best practices
CORRECT:
wp search-replace 'old.com' 'new.com' --skip-columns=guid
4. Not Verifying Database After Import
WRONG:
wp db import backup.sql
WHY THIS FAILS:
- Import might have failed partially
- Database corruption not detected
- Site may be broken
CORRECT:
wp db import backup.sql
wp db check
wp core version
5. Forgetting to Flush Cache/Rewrites
WRONG:
wp rewrite structure '/%postname%/'
WHY THIS FAILS:
- Permalink changes not applied
- 404 errors on pages
- Users see broken links
CORRECT:
wp rewrite structure '/%postname%/' --hard
wp rewrite flush
wp cache flush
6. Using --allow-root Without Understanding Risks
WRONG:
sudo wp plugin install akismet --allow-root
WHY THIS FAILS:
- Creates files owned by root
- WordPress cannot modify those files
- Permission issues
CORRECT:
sudo -u www-data wp plugin install akismet
7. Not Checking Command Success
WRONG:
wp db export backup.sql
wp db reset --yes
wp db import backup.sql
WHY THIS FAILS:
- If export fails, reset still runs
- Database destroyed with no backup
- Catastrophic data loss
CORRECT:
#!/bin/bash
wp db export backup.sql
if [ $? -eq 0 ]; then
echo "Backup successful, proceeding..."
wp db reset --yes
wp db import backup.sql
else
echo "Backup failed! Aborting."
exit 1
fi
Red Flags - Rationalization Detection
| Rationalization | Reality | Correct Action |
|---|
| "Backups take too long" | Data recovery takes longer | Always backup before DB changes |
| "I don't need --dry-run" | Preview prevents disasters | Always dry-run destructive operations |
| "I'll just run it and see" | Irreversible database changes | Test with --dry-run first |
| "--allow-root is easier" | Creates permission nightmares | Use web server user (www-data) |
| "GUIDs don't matter" | Breaks RSS readers | Use --skip-columns=guid |
| "Cache clears automatically" | It doesn't, causes bugs | Always flush cache after changes |
| "The import worked" | Verify, don't assume | Check database integrity after import |
No Exceptions
NEVER skip these safety practices:
- ✅ Backup before database operations - Export SQL before import, reset, search-replace
- ✅ Use --dry-run for destructive operations - Preview changes before applying
- ✅ Verify backups exist - Check file size, verify SQL is valid
- ✅ Skip GUID column in search-replace - Use --skip-columns=guid
- ✅ Flush cache and rewrites after changes - Prevent stale data issues
- ✅ Check command exit codes - Verify success before proceeding
- ✅ Run as web server user - Avoid permission issues
Time pressure is not a valid reason to skip backups.
"It's just a test site" is not a valid reason to skip safety practices.
"I'll be careful" is not a substitute for --dry-run.
WP-CLI Installation and Setup
Installing WP-CLI
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
wp --info
Bash Completion (Optional)
curl https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash -o ~/.wp-completion.bash
echo "source ~/.wp-completion.bash" >> ~/.bashrc
source ~/.bashrc
Integration with This Template
This skill works with:
- fse-block-theme-development skill - Scaffolding themes with WP-CLI
- wordpress-testing-workflows skill - Scaffolding test suites
- wordpress-deployment-automation skill - Deployment scripts using WP-CLI
- frontend-developer agent - Theme setup and development
Complements:
- commit-commands plugin - Git workflows after WP-CLI operations
- github plugin - Integrating WP-CLI with GitHub workflows
WP-CLI Cheat Sheet
Core Commands
wp core download
wp core config
wp core install
wp core update
wp core version
wp core verify-checksums
Plugin Commands
wp plugin install <plugin>
wp plugin activate <plugin>
wp plugin deactivate <plugin>
wp plugin list
wp plugin update <plugin>
wp plugin delete <plugin>
Theme Commands
wp theme install <theme>
wp theme activate <theme>
wp theme list
wp theme update <theme>
wp theme delete <theme>
Database Commands
wp db export <file>
wp db import <file>
wp db reset
wp db check
wp db optimize
wp db query "<sql>"
User Commands
wp user create <login> <email>
wp user list
wp user delete <user>
wp user set-role <user> <role>
Resources
Skill Version: 1.0.0
Last Updated: 2026-01-18
Tested Against: WP-CLI 2.10+, WordPress 6.7+
Testing Methodology: RED-GREEN-REFACTOR (TDD for documentation)