Skip to main content

WP-CLI Aliases For Advanced WordPress Workflow

Allure Web Solutions Code Snippet

WP-CLI is awesome. Setting up aliases is even more awesome! With WP-CLI + aliases you can easily update plugins and themes on the production or staging servers. You can easily sync databases and uploads between development and staging/production. Anything you can think of!

You setup your aliases inside a wp-cli.yml file which resides in the current directory that you’re running the w-cli commands. Alternatively, you can add it do your global WP-CLI config at ~/.wp-cli/config.yml.

My default config looks like this (specifically for Bedrock & Trellis setup):

path: web/wp

@development:
  ssh: vagrant@example.dev/srv/www/example.com/current

@staging:
  ssh: vagrant@staging.example.com/srv/www/staging.example.com/current

@production:
  ssh: admin@example.com/srv/www/example.com/current

@all:
 - @development
 - @staging
 - @production

Update Core/Plugins/Themes on all environments
wp @all core update && wp @all plugin update --all && wp theme update --all

Sync Development DB to Production DB

wp @development db export - | wp @production db import - &&
wp @production search-replace "example.dev" "example.com"

Full Sync Script

Courtesy of the amazing devs at roots.io.

#!/bin/sh

DEVDIR="web/app/uploads/"
DEVSITE="https://example.dev"

PRODDIR="web@example.com:/srv/www/example.com/shared/uploads/"
PRODSITE="https://example.com"

STAGDIR="web@staging.example.com:/srv/www/example.com/shared/uploads/"
STAGSITE="https://staging.example.com"

FROM=$1
TO=$2

case "$1-$2" in
development-production) DIR="up";  FROMSITE=$DEVSITE;  FROMDIR=$DEVDIR;  TOSITE=$PRODSITE; TODIR=$PRODDIR; ;;
development-staging)    DIR="up"   FROMSITE=$DEVSITE;  FROMDIR=$DEVDIR;  TOSITE=$STAGSITE; TODIR=$STAGDIR; ;;
production-development) DIR="down" FROMSITE=$PRODSITE; FROMDIR=$PRODDIR; TOSITE=$DEVSITE;  TODIR=$DEVDIR; ;;
staging-development)    DIR="down" FROMSITE=$STAGSITE; FROMDIR=$STAGDIR; TOSITE=$DEVSITE;  TODIR=$DEVDIR; ;;
*) echo "usage: $0 development production | development staging | production development | production staging" && exit 1 ;;
esac

read -r -p "Would you really like to reset the $TO database and sync $DIR from $FROM? [y/N] " response

if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
  cd ../ &&
  wp "@$TO" db export &&
  wp "@$FROM" db export - | wp "@$TO" db import - &&
  wp "@$TO" search-replace "$FROMSITE" "$TOSITE" &&
  rsync -az --progress "$FROMDIR" "$TODIR"
fi
Usage:

## USAGE
# ./sync.sh
# usage: ./sync.sh development production | development staging | production development | production staging

## EXAMPLES
# ./sync.sh development production
# ./sync.sh production development

 

allure

Author allure

More posts by allure

Leave a Reply

Designed by

best down free | web phu nu so | toc dep 2017