Automating Local WordPress Site Setup with Scripts

db-speeduplocaldev-wpclialfred

WordPress was recently ranked as the second most dreaded technology by over 50,000 developers in the 2016 Stack Overflow Developer Survey. This leads one to wonder— how can WordPress be more developer-friendly? What are the major pain points that developers are experiencing?

While there definitely won’t be a single answer for these questions, there are some things that we can do to make WordPress easier to work with. Today we’ll be looking at ways to speed up WordPress development using tools like the WP-CLI, custom shell scripts, and Alfred.

This short tutorial was written for OS X, but can be easily adapted for Linux and Windows. It also assumes that you have a basic webserver setup (MAMP or XAMPP will do), and that you have WP-CLI installed. Alfred, a productivity app for OS X, is also featured in this article, but isn’t necessary to implement most of the items covered.

Let’s get started!

Setup

Setting up a dedicated folder for your development scripts is not only cleaner than adding them to an existing directory such as /usr/local/bin/, but it also facilitates sharing or syncing your scripts with multiple development environments.

To do this, create your custom script folder:

mkdir ~/Scripts

Then you’ll need to add the folder to your environment PATH by editing your ~/.bash_profile file and adding the following line:

export PATH=/Users/$USER/Scripts:$PATH

Once you’ve saved the ~/.bash_profile, you’ll need to restart your Terminal session or run source ~/.bash_profile for the changes to take effect.

Finally, create a file called config.sh with the following contents in your ~/Scripts folder:

#! /bin/bash

# Path to your custom themes/plugins
PROJECT_PATH="/Users/$USER/Projects"

# Path to your WordPress installs
SITE_PATH="/Users/$USER/Sites"

# Path to your custom Scripts folder
SCRIPT_PATH="/Users/$USER/Scripts"

# Base URL (sites will be setup as subdirectories)
BASE_URL="http://localhost"

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

You will need to tweak the paths and database information to reflect your specific environment. Next, you’ll need to make sure it’s executable by updating the permissions:

chmod +x ~/Scripts/config.sh

Once you’ve configured this file, you’ll be able to include it in any other scripts you make so you can share config between them without duplicating code.

Automating WordPress Installs

Installing WordPress is already a quick process. The famous “5 minute install” has played a major role in it’s success. But with WP-CLI, this can easily be reduced to under 5 seconds.

Create a file named install-wp.sh in your ~/Scripts folder with the following contents:

#! /bin/bash

# Includes your config file
source config.sh

if [ $# -ne 1 ]; then
    echo $0: usage: Destination Name
    exit 1
fi

DEST=$1

# Create the database.
DB_NAME=$(echo $DEST | sed -e 's/-/_/g')
echo "Creating database $DB_NAME..."

mysql -u$DB_USER -p$DB_PASS -e"CREATE DATABASE $DB_NAME"

# Download WP Core.
wp core download --path=$SITE_PATH/$DEST

# Generate the wp-config.php file
wp core config --path=$SITE_PATH/$DEST --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', true);
define('WP_MEMORY_LIMIT', '256M');
PHP

# Install the WordPress database.
wp core install --path=$SITE_PATH/$DEST --url=$BASE_URL/$DEST --title=$DEST --admin_user=test --admin_password=test --admin_email=YOU@YOURDOMAIN.com

Then you’ll need to make the script executable by updating the permissions:

chmod +x ~/Scripts/install-wp.sh

Now when you run the following command in Terminal:

install-wp.sh "desired-site-name"

The script should automatically download the latest version of WordPress and install the database in one simple step.

If you’re like me, you’re also deleting development environments every few days (especially when it’s so pain free to create new ones). Luckily that can be easily automated as well. Create a filed named uninstall-wp.sh in your ~/Scripts folder:

#! /bin/bash

# Include the config file
source config.sh

if [ $# -ne 1 ]; then
    echo $0: usage: Installation name
    exit 1
fi

DEST=$1

read -p "Are you sure you want to delete the files and DB for '$DEST'?" -n 1 -r
echo    # Move to new line
if [[ $REPLY =~ ^[Yy]$ ]]
then

    echo 'Deleting files...'

    # Delete files
    rm -rf $SITE_PATH/$DEST/

    # Delete the database.
    DB_NAME=$(echo $DEST | sed -e 's/-/_/g')
    echo "Deleting database $DB_NAME..."

    mysql -u$DB_USER -p$DB_PASS -e"DROP DATABASE $DB_NAME"

    echo 'WordPress install deleted successfully.'
fi

Again, you’ll need to make the script executable by updating the permissions:

chmod +x ~/Scripts/uninstall-wp.sh

Once you’ve done that, you can run the script like so:

uninstall-wp.sh "wp-install-to-delete"

This will delete all files and data associated with the install after confirming if you really want to go ahead with it (there is no undo button here!). If you’d rather not delete the files, you could easily modify the script to zip them up and archive them instead.

Tying it Together with Alfred

Alfred is a productivity app for OS X that helps you get things done faster by allowing you to run commands and search your computer with a simple hotkey. Think of it as an overhauled alternative to Apple’s Spotlight search.

With the paid Powerpack addon, you can even create workflows that run custom shell or Terminal scripts. For example, you could run a shell script when you enter a keyword. This is much faster than constantly manually opening Terminal, and offers other helpful features including auto-complete.

alfred-example

To run any of the scripts in your ~/Scripts folder in Alfred, you’ll just need to add the > character before the name of the script as shown in the GIF above.

You can also use Alfred to chain together common development tasks. A favorite of mine is a custom workflow to quickly run grunt and grunt watch.

You can set this up by creating a blank workflow called “Grunt” and add a keyword input with the keyword grunt:

Screen Shot 2016-03-22 at 1.15.47 AM

Then add a “Terminal Command” action with the following contents:

cd ~/Projects/{query} && grunt && grunt watch

The {query} part of the command will be replaced with the argument that you provide to the command in Alfred.

Finally, connect the keyword input to the Terminal output to complete the workflow:
Screen Shot 2016-03-22 at 12.59.51 PM

Now opening Alfred and typing grunt test-project will open a new Terminal window, navigate to ~/Projects/test-project, and run grunt && grunt watch in the project directory.

Automate All the Things!

This barely even scratches the surface on what can be automated and improved on in a typical WordPress development workflow, but should be a good starting point for setting up your own scripts and workflows. If you regularly set aside some time to take a look at your most common tasks and see what can be automated, you’ll be surprised at how much time you’re able to save.

What development tricks or workflows have saved you a lot of time? Let us know in the comments below.

About the Author

Matt Shaw

Matt is a WordPress plugin developer located near Philadelphia, PA. He loves to create awesome new tools with PHP, Javascript, and whatever else he happens to get his hands on.

  • Jonathan Perlman

    Hey guys,

    I’ve got a similar script that does that and more for a classroom environment on a server. It allows me to spin up 30 or more installs in a few minutes. Also, it provides the “teacher” with info sheets for the students.

    https://github.com/jpurpleman/WordPress-Stuff/blob/master/wp-install.sh

  • William

    My WordPress init script has a few more dependencies than yours, but it has definitely sped up my start process for development projects. It’s particularly suited to using the Roots Sage framework (http://roots.io/sage) and their Soil plugin:

    https://gist.github.com/ethicka/c1b71e258a88b8523b7f21f164656b88

    • Solid gold. thanks for sharing

    • Josh

      Thank You!!!!

  • I also use a similar script with Alfred for spinning of development sites.

    Hopefully this is just one of many articles Matt will be writing on this subject.

    A few other things I do:

    I know everyone is a fan of VVV, but I like the simplicity of MAMP and how it manages virtual hosts for me. Eventually I’ll move to a VVV/dnsmasq solution (or something similar). But I worry about making too many changes as I can’t afford downtime.

    I also like having remote access to all the sites on my server, so I use no-ip.com to map a domain to my local server. This way designers I work with can have access at anytime (if I need them to) to view progress.

    I also use uptimerobot.com to monitor my local server (just incase it goes down if I’m away).

    And use MailGun for sending mail.

  • I’m using VVV in combination with Site Wizard. Setting up a new site is as easy as typing “vv create –name=mysite” but it has a lot of other options.
    https://github.com/Varying-Vagrant-Vagrants/VVV
    https://github.com/bradp/vv

  • Here is how I set up new sites. https://gist.github.com/kjbrum/04a243995c3334edbd23

    I highly recommend checking out https://github.com/Mixd/wp-deploy.

  • Amit Shah

    Good tip. I also work on managingsite.com to maintain wordpress websites for clients. Securing and updating wordpress sites are important as website owner.

  • I use AMPPS which has a one click install for local site development.

    • I used to use AMPPS, but my issue was always having to manually edit the hosts file and set up virtual hosts. It was always giving me troubles. That’s why I decided to switch to MAMP Pro.

      • I can see got that might happen. I use gasmask for the Mac to easily edit my hosts for there.

  • Thanks for the script Matt. A found a small error in the script: it is missing the ‘–dbhost=$DB_HOST’ argument when running ‘wp core config’.

  • ractoon

    Since I use MAMP I built a GUI tool to make installs easier (and less repetitive with my default info, as well as themes/plugins I used on every site). Recently released it for public use at http://wpexpress.io/

  • Jonas Sandström

    I’m using python and selenium webdriver to automate some of my WordPress pain points.

  • Mauricio

    Thank you very much for your article!!
    It really helped my creating a customized script for my WP development environment.

  • Aman Bansal

    I have created a Bash Script to automate the wordpress installation. You can find the script on github.
    https://github.com/jeoga/wordpress_install_bash_script