Theme Switching with WP-CLI
Theme switching is one of the most common and powerful operations in WordPress management. WP-CLI makes it instant, safe, and scriptable—perfect for testing, troubleshooting, and managing multiple sites.
Understanding Theme Switching
Switching themes in WordPress changes the entire visual appearance and functionality of your site. With WP-CLI, you can:
- Instantly activate any installed theme
- List available themes and their status
- Install and activate themes in one command
- Switch themes across multiple sites simultaneously
- Troubleshoot by reverting to default themes
Listing Themes
Before switching, you need to know what themes are available.
List All Themes
# List all installed themes
wp theme list
Output:
+-------------------+----------+--------+---------+
| name | status | update | version |
+-------------------+----------+--------+---------+
| twentytwentyfour | active | none | 1.0 |
| twentytwentythree | inactive | none | 1.0 |
| my-custom-theme | inactive | none | 1.2.3 |
+-------------------+----------+--------+---------+
List with Specific Fields
# Show only names
wp theme list --field=name
# Show name and status
wp theme list --fields=name,status
# Get only active theme
wp theme list --status=active --field=name
Check Theme Details
# Get detailed information about a specific theme
wp theme get twentytwentyfour
# Get specific field
wp theme get twentytwentyfour --field=version
Activating Themes
Basic Theme Activation
# Activate a theme by name
wp theme activate twentytwentyfour
Output:
Success: Switched to 'Twenty Twenty-Four' theme.
Activate with Verification
# Check if theme exists before activating
wp theme is-installed twentytwentyfour && wp theme activate twentytwentyfour || echo "Theme not found"
Installing and Activating Themes
Install from WordPress.org
# Install a theme from the WordPress repository
wp theme install astra
# Install and activate immediately
wp theme install astra --activate
# Install specific version
wp theme install astra --version=3.9.0
Install from ZIP File
# Install theme from local ZIP file
wp theme install /path/to/theme.zip
# Install from URL
wp theme install https://example.com/themes/custom-theme.zip --activate
Install Multiple Themes
# Install several themes at once
wp theme install astra generatepress kadence --activate
When installing multiple themes, only the last one will be activated. To install without activating:
wp theme install astra generatepress kadence
Common Theme Switching Scenarios
Scenario 1: Emergency Theme Switch (Site Broken)
Problem: A theme update broke your site.
Solution:
# Immediately switch to a default WordPress theme
wp theme activate twentytwentyfour
# Or use the latest default theme
wp theme activate $(wp theme list --status=inactive --name=twenty* --field=name | head -1)
Scenario 2: Testing a New Theme
Workflow:
# 1. Backup current theme name
CURRENT_THEME=$(wp theme list --status=active --field=name)
# 2. Install and activate test theme
wp theme install generatepress --activate
# 3. Test the site...
# 4. Revert to original theme
wp theme activate $CURRENT_THEME
Scenario 3: Switching Themes Across Multiple Sites
For WordPress Multisite:
# Switch theme on all subsites
for site_id in $(wp site list --field=blog_id); do
wp theme activate twentytwentyfour --url=$(wp site list --field=url | sed -n ${site_id}p)
echo "Switched theme for site $site_id"
done
For Multiple Separate Sites:
#!/bin/bash
# switch-themes.sh
sites=(
"/var/www/site1"
"/var/www/site2"
"/var/www/site3"
)
for site in "${sites[@]}"; do
wp theme activate twentytwentyfour --path=$site
echo "Switched theme for $site"
done
Scenario 4: A/B Testing Themes
#!/bin/bash
# ab-test-themes.sh
# Activate Theme A
wp theme activate theme-a
echo "Theme A activated. Test and press Enter to continue..."
read
# Activate Theme B
wp theme activate theme-b
echo "Theme B activated. Test and press Enter to revert..."
read
# Revert to original
wp theme activate theme-a
Advanced Theme Management
Check Theme Status
# Check if a theme is installed
wp theme is-installed astra && echo "Installed" || echo "Not installed"
# Check if a theme is active
wp theme status astra
Get Active Theme Information
# Get active theme name
wp theme list --status=active --field=name
# Get active theme version
ACTIVE_THEME=$(wp theme list --status=active --field=name)
wp theme get $ACTIVE_THEME --field=version
# Get active theme path
wp theme path $ACTIVE_THEME
Update Themes Before Switching
# Update a specific theme before activating
wp theme update astra
wp theme activate astra
# Update all themes
wp theme update --all
Theme Switching Best Practices
1. Always Backup Before Switching
# Backup database before major theme change
wp db export backup-before-theme-switch-$(date +%Y%m%d).sql
# Backup theme files
tar -czf themes-backup-$(date +%Y%m%d).tar.gz wp-content/themes/
2. Check for Theme Dependencies
# Check if theme requires specific plugins
wp theme get astra --field=requires_plugins
# Install required plugins first
wp plugin install elementor --activate
wp theme activate astra
3. Test on Staging First
# On staging environment
wp theme install new-theme --activate
# Test thoroughly...
# On production (after successful staging test)
wp theme install new-theme --activate
4. Document Theme Switches
# Log theme changes
echo "$(date): Switched from $(wp theme list --status=active --field=name) to twentytwentyfour" >> theme-changes.log
wp theme activate twentytwentyfour
Troubleshooting Theme Switches
Theme Won't Activate
# Check for PHP errors
wp theme activate problematic-theme 2>&1 | tee theme-error.log
# Enable debug mode
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
# Try activating again and check logs
wp theme activate problematic-theme
tail -f wp-content/debug.log
Broken Site After Theme Switch
# Immediately revert to default theme
wp theme activate twentytwentyfour
# Check for missing plugins
wp plugin list --status=inactive
# Activate required plugins
wp plugin activate elementor
Theme Not Found
# Verify theme is installed
wp theme list
# Check theme directory exists
ls -la wp-content/themes/
# Reinstall theme
wp theme install theme-name --force
Automating Theme Switches
Scheduled Theme Switch (Cron)
# Add to crontab: Switch to maintenance theme at 2 AM daily
0 2 * * * cd /var/www/html && wp theme activate maintenance-theme
# Switch back at 3 AM
0 3 * * * cd /var/www/html && wp theme activate main-theme
Conditional Theme Switching
#!/bin/bash
# conditional-theme-switch.sh
# Switch theme based on environment
if [ "$ENVIRONMENT" == "production" ]; then
wp theme activate production-theme
elif [ "$ENVIRONMENT" == "staging" ]; then
wp theme activate staging-theme
else
wp theme activate development-theme
fi
Theme Switching in WordPress Multisite
Network-Wide Theme Management
# Enable a theme for the entire network
wp theme enable twentytwentyfour --network
# Disable a theme network-wide
wp theme disable old-theme --network
# Activate theme on specific subsite
wp theme activate twentytwentyfour --url=subsite.example.com
Bulk Subsite Theme Switching
# Switch all subsites to the same theme
wp site list --field=url | xargs -I {} wp theme activate twentytwentyfour --url={}
# Switch only specific subsites
for url in site1.example.com site2.example.com; do
wp theme activate new-theme --url=$url
done
Performance Considerations
Theme Switching Impact
| Aspect | Impact | Mitigation |
|---|---|---|
| Page Cache | Cleared automatically | Rebuild cache after switch |
| Widget Areas | May change/disappear | Document widget configurations |
| Menu Locations | May need reassignment | Use same location names |
| Theme Mods | Lost when switching | Export before switching |
| Custom CSS | Theme-specific, lost | Use child theme or plugin |
Minimize Downtime
# Pre-install theme before switching
wp theme install new-theme
# Switch during low-traffic period
wp theme activate new-theme
# Clear all caches
wp cache flush
wp rewrite flush
Quick Reference
Essential Commands
# List all themes
wp theme list
# Get active theme
wp theme list --status=active --field=name
# Activate theme
wp theme activate twentytwentyfour
# Install and activate
wp theme install astra --activate
# Check if installed
wp theme is-installed astra
# Get theme details
wp theme get astra
# Update theme
wp theme update astra
# Delete theme
wp theme delete old-theme
Common Workflows
# Safe theme switch workflow
CURRENT=$(wp theme list --status=active --field=name)
wp db export backup.sql
wp theme activate new-theme
# Test site...
# If issues: wp theme activate $CURRENT
# Install, test, and revert
wp theme install test-theme --activate
# Test...
wp theme delete test-theme --force
Comparison: WP-CLI vs wp-admin
| Task | WP-CLI | wp-admin |
|---|---|---|
| Speed | Instant | Requires page loads |
| Bulk Operations | Easy (scripts) | One at a time |
| Preview | ❌ No live preview | ✅ Theme Customizer preview |
| Automation | ✅ Fully scriptable | ❌ Manual only |
| Recovery | ✅ Works when site broken | ❌ May be inaccessible |
| Multisite | ✅ Bulk subsite switching | ⚠️ One site at a time |
Use wp-admin's Theme Customizer when you need to preview theme changes before activating. WP-CLI is best for instant switching, troubleshooting, and automation.
Safety Checklist
Before switching themes in production:
- Backup database:
wp db export backup.sql - Backup theme files:
tar -czf themes-backup.tar.gz wp-content/themes/ - Test on staging environment first
- Document current theme and version
- Check for theme-specific plugins
- Verify menu locations are compatible
- Export theme mods:
wp theme mod list --format=json > mods-backup.json - Schedule during low-traffic period
- Have rollback plan ready
Summary
| Aspect | Details |
|---|---|
| Primary Command | wp theme activate THEME_NAME |
| Best For | Emergency fixes, bulk operations, automation |
| Speed | Instant (< 1 second) |
| Safety | Always backup first |
| Multisite | Full support for network and subsites |
| Limitation | No visual preview (use wp-admin for that) |
WP-CLI makes theme switching instant and scriptable. Use it for troubleshooting, testing, and managing multiple sites. For visual design decisions, use wp-admin's Theme Customizer.
Next Steps
- Customize your theme: Theme Customization
- Manage plugins: Plugin Management
- Learn database operations: Database Management