WP-CLI is the most powerful tool for local WordPress development workflows and becomes significantly more productive when combined with aliases, shell functions, and short scripts that automate repetitive operations. WP-CLI aliases defined in ~/.wp-cli/config.yml allow running commands against a remote or staging site with a single flag: wp @staging post list instead of specifying --ssh, --path, and --url flags on every command. Local aliases point to a specific WordPress installation path and are useful when working with multiple projects: wp @mysite option get siteurl works from any directory. The wp search-replace command is the correct way to migrate a database from one domain to another after pulling a staging database locally — it handles serialized PHP strings correctly, unlike a raw SQL REPLACE() call that breaks serialized arrays and objects stored in wp_options and wp_postmeta. Common WP-CLI one-liners worth saving as shell aliases: flushing all caches (wp cache flush && wp rewrite flush && wp transient delete --all), resetting a local admin password (wp user update 1 --user_pass=localpass), activating all plugins at once (wp plugin activate --all), and exporting a clean database dump (wp db export --add-drop-table backup.sql). Shell scripts that wrap WP-CLI commands provide a consistent onboarding flow for new team members: a single ./scripts/setup-local.sh that runs wp search-replace, activates the right plugins, creates a test admin user, and imports sample WooCommerce products eliminates the “works on my machine” problem. The wp eval-file command runs a PHP script in the WordPress context without a web request, which is useful for one-off data migrations, bulk meta updates, and testing hooks in isolation. The Git hooks post shows how to integrate WP-CLI commands into the development workflow at the commit and push stages.
Problem: Setting up a local WordPress environment from a staging database pull requires 15–20 manual steps — domain replacement, plugin activation, admin user creation, and permalink flushing — that take 10–15 minutes and are error-prone when done manually each time.
Solution: Define WP-CLI aliases in ~/.wp-cli/config.yml for each environment, create shell function aliases for the most common command sequences, and write a setup-local.sh script that automates the full pull-and-configure workflow in one command.
# ~/.wp-cli/config.yml
# Global WP-CLI settings
path: /var/www/html
color: true
# Environment aliases
aliases:
local:
path: /Applications/XAMPP/xamppfiles/htdocs/mysite
url: http://mysite.local
staging:
ssh: deploy@staging.example.com
path: /var/www/staging
url: https://staging.example.com
production:
ssh: deploy@example.com
path: /var/www/production
url: https://example.com
# Usage:
# wp @local option get siteurl
# wp @staging db export backup.sql
# wp @production cache flush
#!/usr/bin/env bash
# scripts/setup-local.sh — pull staging DB and configure for local dev
set -euo pipefail
STAGING_URL="https://staging.example.com"
LOCAL_URL="http://mysite.local"
WP_PATH="/Applications/XAMPP/xamppfiles/htdocs/mysite"
DUMP="/tmp/staging-$(date +%Y%m%d-%H%M%S).sql"
echo "1/6 Exporting staging database..."
wp @staging db export "$DUMP"
echo "2/6 Importing into local database..."
wp --path="$WP_PATH" db import "$DUMP"
rm "$DUMP"
echo "3/6 Replacing domain..."
wp --path="$WP_PATH" search-replace "$STAGING_URL" "$LOCAL_URL" --all-tables
echo "4/6 Activating development plugins..."
wp --path="$WP_PATH" plugin activate query-monitor debug-bar
wp --path="$WP_PATH" plugin deactivate wordfence # disable security plugin locally
echo "5/6 Creating local admin user..."
wp --path="$WP_PATH" user create localadmin local@dev.test \
--role=administrator --user_pass=localpassword123 2>/dev/null || \
wp --path="$WP_PATH" user update localadmin --user_pass=localpassword123
echo "6/6 Flushing caches and rewrite rules..."
wp --path="$WP_PATH" cache flush
wp --path="$WP_PATH" rewrite flush
wp --path="$WP_PATH" transient delete --all
echo "Done. Local site: $LOCAL_URL admin: localadmin / localpassword123"
# Useful WP-CLI aliases to add to ~/.zshrc or ~/.bashrc:
# alias wpl="wp --path=/Applications/XAMPP/xamppfiles/htdocs/mysite"
# alias wp-reset='wpl cache flush && wpl rewrite flush && wpl transient delete --all'
# alias wp-pull='bash ~/projects/mysite/scripts/setup-local.sh'
NOTE: The wp search-replace command correctly handles serialized PHP data by unserializing, replacing, and re-serializing each value — but it cannot handle object classes that are not loaded during the WP-CLI bootstrap. If search-replace reports errors for specific tables, add --precise to enable regex-based replacement that handles edge cases in malformed serialized strings, or use --skip-tables=wp_custom_table to exclude tables containing binary or non-serialized data.