Bash, VV, and WP-CLI to start new WordPress sites

Bash scripting to automate site builds with WP-CLI

I told myself that I would endeavor to create a project using an entirely new-to-me technology every month, and on the last possible day in January I’m doing a writeup on a bit of bash and wp-cli code written for last week’s WordPress Orlando Meetup.

While I didn’t quite hit the mark, I did learn quite a bit more about WP-CLI and bash scripting, so I’ll count that as a win.

The script that we’re going to refer to in the post is here: This is intended as a follow up to this post on using VVV, VV, and WP-CLI to setup new sites.

What are we doing here?

The script does quite a few things to set up a new WordPress site with some defaults. I made the selections based on my common usage, but the script has been made general enough that it can be modified as you see fit for your own needs.

First, I’ve got a few constants in the script, including a username that I like to use for these sites, so I get something other than “admin” that isn’t random. It also sets an email for all accounts, and the specific VV blueprint that I want to use. I can cover blueprints at a later time, but for now check out some information about them at the VV Github page.

Next up I use the name that was input after the command to run this script was issued to create the name and domain of the website. A password and table prefix (the wp_ portion of the WordPress database tables) are both randomly generated. The password is copied to the clipboard to allow me to paste it in while logging into the site for the first time.

The command below does quite a bit. It uses VV to create a new site on my VVV install, with the name and domain supplied, the username and email that I pre-set, and the table prefix and password that were just generated for us. It also turns on debug, calls the VV blueprint that I’ve setup, and tells VV to use all other defaults that it normally supplies.

yes | vv create -n $name -d $ --username $wpuser --password $password --email $email --prefix $prefix -x -b $blueprint --defaults

VV and the blueprint do the bulk of the initial setup, but there are still a lot of things to do.

Continuing Site Setup With WP-CLI

There are a lot of tasks that I do on almost every site, most outlined on the previous post, but as a recap:

  • Deleting the “Hello Dolly” plugin as well as all default themes except for the current year default
  • Activating the premium plugins and themes that I use. This includes the Genesis theme, as well as Gravity Forms, iThemes Security and Sync, BackupBuddy, and Advanced Custom Field.
  • Deleting the default page, post, and comment
  • Creating home and blog pages, and setting them to display as home and blog, respectively
  • Creating About and Contact pages
  • Creating a menu with all of the pages that I’ve created, and setting it to the primary menu
  • Removing all default widgets from the sidebar
  • Creating a category titled “News”, and setting it as the default category, to avoid posts being labeled as uncategorized

Finally, I have the script open up Chrome to the login page for the site that was just created. There I can type in my username and paste in the generated password. Eventually this step can be scripted as well, to automatically log me in.

And there we have it: a fully setup staging site that saves us hours on creation and setting up defaults that will be used over and over again!

Drawbacks to the current bash script method

That’s not to say that this is perfect. By all accounts there’s plenty that I can do to fix up this script. I started working on an integration with the Lastpass-CLI for instance, to use their password generator and to automatically save my passwords to avoid being prompted when I first load the site. For some reason I was unable to get it working, and removed that feature to get it done in time for the meetup

Gravity Forms also has a CLI, though with the downside of needing to be installed as a plugin first. If I install the plugin though, I can use that to install and license my copy of Gravity Forms, as well as import some default forms. I’m going to add that to a future version of this builder, as most sites that I make will have a contact and newsletter form with a few fields by default.

Finally, I want to work on executing the script on the server, as opposed to on my local machine. I can set this up with a bit of forethought, but it’d be beneficial to have some sort of installer to allow anyone to input their specifics (credentials, email, install directory, plugins to activate, etc). For now, I’m using vassh, which was developed specifically to run

vagrant ssh

followed by a WP-CLI command, but the tool is not very efficient. Currently it opens and closes a connection with each command, which quickly adds up when you’re running several dozen commands like this script is.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.