From 30 Minutes to 10 Seconds: Automating Wordpress Setup

by Greg Parker

As a developer, whenever I find myself repeating a task several times my first thought is: “Can I automate this?”

We build a lot of sites on WordPress. As you know, there are many initial steps to getting the local dev environment setup. To minimize these steps, I utilized the WP CLI tool and wrote a shell script.

Below is a walkthrough of the different commands. Customize it to fit your needs, maybe use some of the time you save to take a power nap, you deserve it (you’re welcome).

Files

I keep all of my projects in ~/Sites and put the WordPress files in a wp directory. With the DNS Masq tool, I can use the .wp extension to view the site. For example, if I have ~/Sites/project/wp then I can use http://project.wp.

The first thing I do: create the project directory and get the most recent version of WordPress.

Once those files are extracted, I get the most recent version of the HTML5Blank theme which I like to use as a starting my point for all my projects.

# Setup site folder
mkdir ~/Sites/$1
cd ~/Sites/$1
wget http://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress/ ~/Sites/$1/wp
rm latest.tar.gz

# Copy starter theme
cd ~/Sites/$1
wget http://github.com/toddmotto/html5blank/archive/stable.zip
unzip stable.zip
mv html5blank-stable/ ~/Sites/$1/wp/wp-content/themes/$1
rm stable.zip

Database

Now it’s time to setup the database so that the WordPress install will run correctly. I need to create a new database, a new user, and grant all privileges to that user.

I like to keep the database name and user the same as the project name. It helps with consistency and ensures that I don’t have duplicates.

# Setup MySQL Database
mysql.server start
mysql -u root -e “create database $1”;
mysql -u root -e “create user ‘$1’@’localhost’ identified by password ‘’”;
mysql -u root -e “GRANT ALL PRIVILEGES ON $1.* TO $1@localhost”;
mysql -u root -e “FLUSH PRIVILEGES”;

Install

After I have the files and database setup, it’s time to create the config file and run the install. For this, I use the WP CLI tool which allows you to perform common WordPress tasks from the command line. Super helpful when writing a shell script.

For local environments, I keep the admin login as admin/admin but this should obviously change for production.

# Create config file
cd ~/Sites/$1/wp
wp core config — dbname=$1 — dbuser=$1
wp core install — url=http://$1.wp — title=$1 — admin_user=admin — admin_password=admin — admin_email=greg@brllnt.co

Activate

After the install is successful, I activate my starter theme, any plugins, and clean up the file system.

WordPress comes pre-installed with a couple of themes and plugins which I don’t use, so I get rid of them.

Since I use the Advanced Custom Fields plugin across every project, I copy that over and activate it.

# Install and activate theme and plugins
wp theme activate $1
rm -rf ~/Sites/$1/wp/wp-content/themes/twentyfourteen
rm -rf ~/Sites/$1/wp/wp-content/themes/twentyfifteen
rm -rf ~/Sites/$1/wp/wp-content/themes/twentysixteen
rm -rf ~/Sites/$1/wp/wp-content/plugins/akismet
rm ~/Sites/$1/wp/wp-content/plugins/hello.php
mkdir ~/Sites/$1/wp/wp-content/plugins/advanced-custom-fields-pro
cp -r ~/wordpress-plugins/advanced-custom-fields-pro/* ~/Sites/$1/wp/wp-content/plugins/advanced-custom-fields-pro
wp plugin activate advanced-custom-fields-pro

Conclusion

While I’m not yet on the level of this guy, being able to setup a WordPress environment in 10 seconds instead of 30 minutes has been a huge time saver for me. I plan to continue to extend this script to my needs. And, maybe have it build from a JSON config file? I’ll keep you posted.