DEV Community

Cover image for Automating WordPress Setup with WP‑CLI for Repeat Projects
Martijn Assie
Martijn Assie

Posted on

Automating WordPress Setup with WP‑CLI for Repeat Projects

Client asks: "Can you build 5 landing pages for our regional offices - same design, different content?"

Dashboard clicking: 6 hours configuring each site manually!!

WP-CLI script: 8 minutes total, identical setup every time!!

Stopped clicking around wp-admin three years ago. Built one bash script that installs WordPress, themes, plugins, creates pages, sets permalinks - everything - in under 60 seconds per site!!

Here's how to automate your entire WordPress workflow:

Why WP-CLI Destroys Manual Setup

Manual WordPress install (clicking):

  • Download WordPress
  • Upload files
  • Create database
  • Run installer
  • Configure settings
  • Install theme
  • Install 12 plugins
  • Activate plugins
  • Create pages
  • Set permalinks
  • Configure options
  • Delete default content
  • Total: 45-60 minutes per site!!

WP-CLI automated script:

bash setup-wp.sh clientname
Enter fullscreen mode Exit fullscreen mode
  • Total: 45 seconds!!

Same result. 98% time savings!!

Installing WP-CLI

Check Requirements

Need SSH access to server (most quality hosts provide this).

Test SSH connection:

ssh username@yourserver.com
Enter fullscreen mode Exit fullscreen mode

Check PHP version (need 5.6+):

php -v
Enter fullscreen mode Exit fullscreen mode

Download WP-CLI

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Enter fullscreen mode Exit fullscreen mode

Make executable:

chmod +x wp-cli.phar
Enter fullscreen mode Exit fullscreen mode

Move to system path:

sudo mv wp-cli.phar /usr/local/bin/wp
Enter fullscreen mode Exit fullscreen mode

Test installation:

wp --info
Enter fullscreen mode Exit fullscreen mode

Should output:

OS:     Linux
Shell:  /bin/bash
PHP binary:    /usr/bin/php
PHP version:   8.1.12
Enter fullscreen mode Exit fullscreen mode

You're ready!!

Basic WP-CLI Commands

Install WordPress Core

wp core download
Enter fullscreen mode Exit fullscreen mode

Downloads latest WordPress to current directory.

Specific version:

wp core download --version=6.4
Enter fullscreen mode Exit fullscreen mode

Create wp-config.php

wp core config \
  --dbname=database_name \
  --dbuser=database_user \
  --dbpass=database_password \
  --dbhost=localhost
Enter fullscreen mode Exit fullscreen mode

Install WordPress

wp core install \
  --url=https://example.com \
  --title="Site Title" \
  --admin_user=admin \
  --admin_password=secure_password \
  --admin_email=admin@example.com
Enter fullscreen mode Exit fullscreen mode

That's it!! WordPress installed!!

Theme Management

List installed themes:

wp theme list
Enter fullscreen mode Exit fullscreen mode

Install theme:

wp theme install astra --activate
Enter fullscreen mode Exit fullscreen mode

Install from zip:

wp theme install https://example.com/custom-theme.zip --activate
Enter fullscreen mode Exit fullscreen mode

Plugin Management

Install single plugin:

wp plugin install contact-form-7 --activate
Enter fullscreen mode Exit fullscreen mode

Install multiple plugins:

wp plugin install \
  yoast-seo \
  wordfence \
  wp-super-cache \
  akismet \
  --activate
Enter fullscreen mode Exit fullscreen mode

Deactivate all plugins (debugging!):

wp plugin deactivate --all
Enter fullscreen mode Exit fullscreen mode

Building Your First Automation Script

Let me show you my actual production script (simplified version).

Create setup-wp.sh File

#!/bin/bash

# WordPress Installation Script
# Usage: bash setup-wp.sh sitename

# Get site name from argument
SITENAME=$1

if [ -z "$SITENAME" ]; then
    echo "Error: Please provide site name"
    echo "Usage: bash setup-wp.sh sitename"
    exit 1
fi

[![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbzvpqo3onjul2aqkhu1.webp)](https://www.elegantthemes.com/affiliates/idevaffiliate.php?id=53476&url=73342)

# Configuration
DB_NAME="${SITENAME}_db"
DB_USER="root"
DB_PASS="root"
SITE_URL="http://localhost/$SITENAME"
ADMIN_USER="admin"
ADMIN_PASS="Admin123!"
ADMIN_EMAIL="admin@example.com"

echo "Starting WordPress installation for: $SITENAME"

# Create directory
mkdir -p $SITENAME
cd $SITENAME

# Download WordPress
echo "Downloading WordPress..."
wp core download

# Create database
echo "Creating database..."
wp db create

# Generate wp-config
echo "Configuring WordPress..."
wp core config \
  --dbname=$DB_NAME \
  --dbuser=$DB_USER \
  --dbpass=$DB_PASS

# Install WordPress
echo "Installing WordPress..."
wp core install \
  --url=$SITE_URL \
  --title="$SITENAME" \
  --admin_user=$ADMIN_USER \
  --admin_password=$ADMIN_PASS \
  --admin_email=$ADMIN_EMAIL

echo "WordPress installed successfully!"
echo "URL: $SITE_URL"
echo "Username: $ADMIN_USER"
echo "Password: $ADMIN_PASS"
Enter fullscreen mode Exit fullscreen mode

Make executable:

chmod +x setup-wp.sh
Enter fullscreen mode Exit fullscreen mode

Run it:

bash setup-wp.sh testsite
Enter fullscreen mode Exit fullscreen mode

60 seconds later: WordPress installed!!

Advanced Script: Complete Site Setup

This installs WordPress + theme + plugins + pages + settings!!

#!/bin/bash

# Advanced WordPress Setup Script
# Installs everything you need for client sites

SITENAME=$1

if [ -z "$SITENAME" ]; then
    echo "Usage: bash advanced-setup.sh sitename"
    exit 1
fi

# Configuration
DB_NAME="${SITENAME}_db"
DB_USER="root"
DB_PASS="root"
SITE_URL="http://localhost/$SITENAME"
ADMIN_USER="admin"
ADMIN_PASS=$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 16)
ADMIN_EMAIL="admin@example.com"

echo "🚀 Setting up WordPress site: $SITENAME"

# Create and navigate to directory
mkdir -p $SITENAME && cd $SITENAME

# Download WordPress
echo "📥 Downloading WordPress..."
wp core download --quiet

# Create wp-config with debug settings
echo "⚙️  Configuring WordPress..."
wp core config \
  --dbname=$DB_NAME \
  --dbuser=$DB_USER \
  --dbpass=$DB_PASS \
  --extra-php <<PHP
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('DISALLOW_FILE_EDIT', true);
PHP

# Create database
echo "💾 Creating database..."
wp db create

# Install WordPress
echo "🔧 Installing WordPress..."
wp core install \
  --url=$SITE_URL \
  --title="$SITENAME" \
  --admin_user=$ADMIN_USER \
  --admin_password=$ADMIN_PASS \
  --admin_email=$ADMIN_EMAIL \
  --skip-email

# Install and activate theme
echo "🎨 Installing theme..."
wp theme install astra --activate

# Install essential plugins
echo "🔌 Installing plugins..."
wp plugin install \
  contact-form-7 \
  yoast-seo \
  wordfence \
  wp-super-cache \
  classic-editor \
  --activate

[![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbzvpqo3onjul2aqkhu1.webp)](https://www.elegantthemes.com/affiliates/idevaffiliate.php?id=53476&url=73342)

# Set permalink structure
echo "🔗 Setting permalinks..."
wp rewrite structure '/%postname%/' --hard

# Delete default content
echo "🗑️  Removing default content..."
wp post delete 1 --force  # Hello World post
wp post delete 2 --force  # Sample page
wp comment delete 1 --force

# Create essential pages
echo "📄 Creating pages..."
wp post create \
  --post_type=page \
  --post_title='Home' \
  --post_status=publish \
  --post_content='Welcome to our website'

wp post create \
  --post_type=page \
  --post_title='About' \
  --post_status=publish

wp post create \
  --post_type=page \
  --post_title='Services' \
  --post_status=publish

wp post create \
  --post_type=page \
  --post_title='Contact' \
  --post_status=publish

# Set homepage
HOME_ID=$(wp post list --post_type=page --post_title="Home" --field=ID)
wp option update page_on_front $HOME_ID
wp option update show_on_front page

# Configure site settings
echo "⚙️  Configuring settings..."
wp option update blogdescription "Professional website"
wp option update timezone_string "America/New_York"
wp option update date_format "F j, Y"
wp option update time_format "g:i a"

# Disable comments on pages
wp option update default_comment_status closed

# Set image sizes
wp option update thumbnail_size_w 300
wp option update thumbnail_size_h 300
wp option update medium_size_w 768
wp option update medium_size_h 768
wp option update large_size_w 1024
wp option update large_size_h 1024

# Flush cache
wp cache flush

echo ""
echo "✅ WordPress setup complete!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "URL:      $SITE_URL"
echo "Username: $ADMIN_USER"
echo "Password: $ADMIN_PASS"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "Next steps:"
echo "1. Visit $SITE_URL/wp-admin"
echo "2. Login with credentials above"
echo "3. Start customizing!"
Enter fullscreen mode Exit fullscreen mode

This creates production-ready WordPress in 90 seconds!!

Pro Tips: Making Scripts Reusable

Use Config File

Create config.sh with your defaults:

#!/bin/bash

# Default configuration for all sites
DEFAULT_THEME="astra"
DEFAULT_PLUGINS=(
  "contact-form-7"
  "yoast-seo"
  "wordfence"
  "wp-super-cache"
)

[![ ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbzvpqo3onjul2aqkhu1.webp)](https://www.elegantthemes.com/affiliates/idevaffiliate.php?id=53476&url=73342)

# Database settings
DB_USER="root"
DB_PASS="root"
DB_HOST="localhost"

# Admin defaults
ADMIN_USER="admin"
ADMIN_EMAIL="admin@example.com"

# Site settings
TIMEZONE="America/New_York"
PERMALINK_STRUCTURE="/%postname%/"
Enter fullscreen mode Exit fullscreen mode

Then source it in scripts:

#!/bin/bash
source config.sh

# Now use variables
wp theme install $DEFAULT_THEME --activate
Enter fullscreen mode Exit fullscreen mode

Create Uninstall Script

Delete sites just as fast!!

#!/bin/bash

# Uninstall WordPress Script
# Usage: bash uninstall-wp.sh sitename

SITENAME=$1

if [ -z "$SITENAME" ]; then
    echo "Usage: bash uninstall-wp.sh sitename"
    exit 1
fi

DB_NAME="${SITENAME}_db"
DB_USER="root"
DB_PASS="root"

echo "⚠️  This will DELETE everything for: $SITENAME"
read -p "Are you sure? (y/n) " -n 1 -r
echo

if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "🗑️  Deleting files..."
    rm -rf $SITENAME

    echo "💾 Dropping database..."
    wp db drop --dbname=$DB_NAME --yes

    echo "✅ Site deleted successfully!"
else
    echo "Cancelled."
fi
Enter fullscreen mode Exit fullscreen mode

Managing Multiple Sites

Update All Sites Script

#!/bin/bash

# Update WordPress core, themes, and plugins across all sites
# Usage: bash update-all.sh

SITES=(
  "/var/www/site1"
  "/var/www/site2"
  "/var/www/site3"
)

for SITE in "${SITES[@]}"; do
    echo "Updating $SITE..."
    cd $SITE

    wp core update
    wp theme update --all
    wp plugin update --all
    wp db optimize

    echo "✅ $SITE updated"
    echo ""
done

echo "All sites updated!"
Enter fullscreen mode Exit fullscreen mode

Run monthly = all sites updated in minutes!!

Backup Script

#!/bin/bash

# Backup WordPress database and files
# Usage: bash backup-wp.sh sitename

SITENAME=$1
BACKUP_DIR="$HOME/wp-backups"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

mkdir -p $BACKUP_DIR

cd $SITENAME

# Export database
echo "📦 Backing up database..."
wp db export "$BACKUP_DIR/${SITENAME}_${TIMESTAMP}.sql"

# Backup files
echo "📦 Backing up files..."
tar -czf "$BACKUP_DIR/${SITENAME}_${TIMESTAMP}.tar.gz" \
  --exclude='wp-content/cache/*' \
  --exclude='wp-content/uploads/cache/*' \
  .

echo "✅ Backup complete!"
echo "Location: $BACKUP_DIR"
Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

Client Site Template

My actual client site script (sanitized):

#!/bin/bash

# Client Site Setup - Used for all new clients
# Creates WordPress with our standard stack

CLIENTNAME=$1
SITE_URL="https://${CLIENTNAME}.com"

# Install WordPress
wp core download
wp core config --dbname=${CLIENTNAME}_prod --dbuser=prod_user --dbpass=$DB_PASS
wp db create
wp core install --url=$SITE_URL --title="$CLIENTNAME" --admin_user=admin --admin_password=$SECURE_PASS --admin_email=admin@agency.com

# Our standard theme + child theme
wp theme install generatepress --activate
wp scaffold child-theme ${CLIENTNAME}-child --parent_theme=generatepress --activate

# Essential plugins
wp plugin install \
  wp-rocket \
  wordfence \
  updraftplus \
  wp-mail-smtp \
  gravity-forms \
  advanced-custom-fields-pro \
  --activate

# Pages every client needs
wp post create --post_type=page --post_title='Home' --post_status=publish
wp post create --post_type=page --post_title='About Us' --post_status=publish
wp post create --post_type=page --post_title='Services' --post_status=publish
wp post create --post_type=page --post_title='Contact' --post_status=publish
wp post create --post_type=page --post_title='Privacy Policy' --post_status=publish

# Set homepage
HOME_ID=$(wp post list --post_type=page --title="Home" --field=ID)
wp option update page_on_front $HOME_ID
wp option update show_on_front page

# Security settings
wp option update users_can_register 0
wp config set DISALLOW_FILE_EDIT true --raw

# Performance
wp rewrite structure '/%postname%/' --hard
wp rewrite flush

echo "Client site ready for development!"
Enter fullscreen mode Exit fullscreen mode

15 sites per month = saves 60 hours!!

Development to Production Migration

#!/bin/bash

# Migrate from dev to production
# Usage: bash migrate-to-prod.sh

DEV_URL="http://localhost/mysite"
PROD_URL="https://mysite.com"

# Export dev database
wp db export dev-backup.sql

# Search-replace URLs
wp search-replace $DEV_URL $PROD_URL --export=prod-ready.sql

# Upload to production server (via SCP)
scp prod-ready.sql user@production:/var/www/mysite/

# SSH to production and import
ssh user@production "cd /var/www/mysite && wp db import prod-ready.sql"

echo "Migration complete!"
Enter fullscreen mode Exit fullscreen mode

Debugging WP-CLI Scripts

Common Issues

"wp: command not found"

Check installation:

which wp
Enter fullscreen mode Exit fullscreen mode

Add to PATH if needed:

export PATH=$PATH:/usr/local/bin
Enter fullscreen mode Exit fullscreen mode

Database connection errors

Test database separately:

wp db check
Enter fullscreen mode Exit fullscreen mode

Permission errors

Run with correct user:

sudo -u www-data wp ...
Enter fullscreen mode Exit fullscreen mode

Dry Run Mode

Test without executing:

wp plugin install yoast-seo --dry-run
Enter fullscreen mode Exit fullscreen mode

Verbose Output

See exactly what's happening:

wp core download --debug
Enter fullscreen mode Exit fullscreen mode

Productivity Multipliers

Aliases in .bashrc

# Add to ~/.bashrc

alias wpnew='bash ~/scripts/setup-wp.sh'
alias wpdel='bash ~/scripts/uninstall-wp.sh'
alias wpupdate='bash ~/scripts/update-all.sh'
alias wpbackup='bash ~/scripts/backup-wp.sh'
Enter fullscreen mode Exit fullscreen mode

Now just type:

wpnew clientsite
Enter fullscreen mode Exit fullscreen mode

Instead of:

bash /home/user/scripts/setup-wp.sh clientsite
Enter fullscreen mode Exit fullscreen mode

Tab Completion

Install WP-CLI bash completion:

curl https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash > wp-completion.bash
source wp-completion.bash
Enter fullscreen mode Exit fullscreen mode

Add to .bashrc:

source ~/wp-completion.bash
Enter fullscreen mode Exit fullscreen mode

Now tab completion works:

wp plugin inst[TAB]
# Autocompletes to: wp plugin install
Enter fullscreen mode Exit fullscreen mode

Time Savings Calculator

My actual numbers (freelance developer):

Before WP-CLI:

  • New site setup: 60 minutes
  • Plugin updates (10 sites): 45 minutes weekly
  • Database backups: 20 minutes weekly
  • Client site clones: 90 minutes each

After WP-CLI:

  • New site setup: 2 minutes
  • Plugin updates: 3 minutes weekly
  • Database backups: 1 minute weekly
  • Client site clones: 5 minutes each

Monthly time savings: 22 hours!!

Annual value at $100/hour: $26,400!!

Bottom Line

Stop clicking around wp-admin!! WP-CLI + bash scripts = WordPress automation that actually works!!

My workflow now:

  1. Client requests new site
  2. Run script: wpnew clientname
  3. 90 seconds later: production-ready WordPress
  4. Start actual development immediately

No more:

  • Clicking through installers
  • Manually configuring settings
  • Installing plugins one by one
  • Creating pages manually
  • Forgetting essential plugins

Just fast, consistent, automated WordPress deployments!!

Download WP-CLI today - your future self will thank you!! 🚀

Useful Resources:

Top comments (0)