We love WP-CLI. This powerful command line tool allows us to quickly stand up new WordPress sites from the command line in just a few minutes.

We do so by setting up our wp commands in a Bash script. Naturally, the first step to creating this script is to open things up with a shebang!

Next we’ll need to assign a few variables that we’ll need later in the script:

The above variables reference two files,  wp-cli.yml  and .my.cnf . We’ll circle back to those later.

Our script should exit if there’s already a WordPress install in the current directory, to prevent accidentally overwriting an existing WordPress install. We can achieve this by checking for the existence of wp-config.php:

While we’re at it we should check to ensure that the wp command is available:

We’ll also need to make sure that the aforementioned wp-cli.yml file exists. We keep this file in the .bin directory in our home directory, but if it’s not there then we can’t copy it over to the current directory. Another simple file exists check will do the job:

Next up we’ll want to parse the command line options, to allow us to perform commands or output text based on the user’s provided options.

In the above switch case, we’re checking for long form and shorthand versions of two options. If the user provides the -b or --beta option, we’ll set the beta variable to true so that we know to install the beta version of WordPress later on in the script. If the user specifies either the -h or the --help option, we’ll output some help text to let them know what this script’s options are.

Now it’s time to catch user input. This will allow us to specify some sane default values, but still allow the user running the script to specify another value they’d prefer. For this we’ll use the read command:

One thing to note here is the WordPress Version variable. By default, it’s value is set to latest. This will simply install the latest version of WordPress, which is typically what you’d want. However, in certain situations you may want to install a specific version of WordPress, for example if you’re standing up a WordPress install locally to mimic an existing site for debugging purposes. In this case, you could just enter the specific version number, for example you could type: 4.9.9.

Now that we’re capturing user input and saving it to our variables, we’re ready to copy over the wp-cli.yml file. This is a YAML configuration file that WP-CLI can read options from.

Next up we’re going to need to get the MySQL password so we can create the database for our new WordPress install. We use a MySQL configuration file in .my.cnf to store this password so that we aren’t putting passwords on the command line where they can accessed via history:

We’re now ready to use WP-CLI to install WordPress:

Now we have WordPress installed, but we can run a few automated customizations at this point if we want. First up let’s remove the default plugins that WordPress ships with:

Next let’s set the rewrite structure and flush our URL rewrites:

Finally, we output a confirmation to the user that WordPress has been installed:

A quick note on the two external files our script references. First up is the .my.cnf file which should exist in your user’s home directory. It’s contents should look like this, replacing “passwordgoeshere” with your MySQL user’s actual password, and replacing “root” with your MySQL user’s username:

Please note that there should not be a space between client and root in the above syntax. If your MySQL user is root, then you’d specify that line as [clientroot].

The other file is the wp-cli.yml file. In our case we are just using it to specify our use of the mod_rewrite Apache module. This file should contain the following:

Of course, all of this is just the beginning. You could expand upon this Bash script in many ways, for example to copy over a starter theme that you prefer to use as a starting point for your new WordPress projects, or to copy over and then activate some plugins that you always use.