WP-CLI is a very powerful command-line interface for WordPress. Using Bash we can create a set of shell scripts to quickly set up a new WordPress site to use as a starting point for our new projects. Typically we would do this in our local development environment to quickly get started with a new project.

As always, we’ll start out our Bash script with a shebang:

Next we’ll create some variables so we can quickly configure the details of the new site:

We run a switch case inside of a while loop with a shift at the end to loop through and parse any user-provided options to our script:

We can’t run this script if we don’t have the MySQL config present, and WP-CLI and GruntJS installed, so let’s check to make sure we have everything we need to run:

Next we must get the password for our MySQL root user and assign it into a variable:

We need to make sure our MySQL root user’s password is not blank, otherwise we may run into errors later on in the script:

Within the else section of the above conditional, we can continue to execute the script. We download the WordPress core files with the following command:

We can set the WordPress configuration options and also add some custom configuration directives to the wp-config.php file with the following command:

As you can see, we use our configuration variables from earlier in the script to get the MySQL credentials. Now that everything is properly configured, let’s create the database:

Finally we can install WordPress with:

At this point we have a working WordPress install, using the default theme that ships with WordPress. We can still automate adding and activating our own custom theme, configuring plugin options, creating posts and comments and much more.

For a full detailed example check out our wp-grunt-install  repository on Github:

https://github.com/websightdesigns/wp-grunt-install