With every new site or project, there comes a new WordPress install, and a few steps are needed before you can start to work. They usually involve one or more of the following:
- download / update WordPress
- create a new database and database user
- set up Apache vhost
- set up vhost url on system hosts file
- install your favorite plugins
- install a theme
This is a fairly simple process, which takes up a few minutes every time you start on a new project. However, since I like to automate things to decrease the chance of screwing up, and saving time, I’ve started to play around to see if I could somehow streamline this process.
While I’ve seen people come up with scripts to automate command-line stuff with WordPress, I haven’t seen anything near as complete, or as supported by the community as wp-cli. It was only natural to try using it for the task at hand.
I like to start work on a new website or theme by using Underscores, and thus wanted the script to install and activate the theme alongside the latest version of WordPress. I spend most of the time just trying to get a downloadable file from their website automatically but eventually came to a solution using curl to get it.
After showing it around to friends I got the feedback that it would be handy to have it install a list of favorite plugins. Here is how the script ended up:
#!/bin/bash | |
# test the number of arguments | |
if [ ! $# == 9 ]; then | |
echo "Usage: $0 project_name path dbname dbuser dbpass url admin_email admin_password theme_slug favorite_plugins_file" | |
exit | |
fi | |
# directory that will hold the WordPress files and be the document root for your site | |
PROJECT_NAME="$1" | |
WP_DIR="$2" | |
DBNAME="$3" | |
DBUSER="$4" | |
DBPASS="$5" | |
WP_URL="$6" | |
ADMIN_EMAIL="$7" | |
ADMIN_PASSWORD="$8" | |
THEME_SLUG="$9" | |
# EDIT DEFAULT VALUES HERE | |
: ${PROJECT_NAME:="wptest"} | |
: ${WP_DIR:="/Users/nuno/Projects/".$PROJECT_NAME} | |
: ${DBNAME:="$PROJECT_NAME"} | |
: ${DBUSER:="user"} | |
: ${DBPASS:="pass"} | |
: ${WP_URL:="$PROJECT_NAME.dev"} # the url you will use when accessing this on localhost, e.g. http://wptest.dev/ | |
: ${ADMIN_EMAIL:="email"} | |
: ${ADMIN_PASSWORD:="pass"} | |
: ${THEME_SLUG:="$PROJECT_NAME"} | |
: ${FILE_FAV_PLUGINS:="favorite-plugins.txt"} | |
# Detect paths | |
CAT=$(which cat) | |
AWK=$(which awk) | |
GREP=$(which grep) | |
PLUGINS=$($CAT $FILE_FAV_PLUGINS | $GREP -v ^# ) | |
mkdir $WP_DIR; cd $WP_DIR | |
# to-do: check if wp-cli is available | |
wp core download | |
wp core config --dbname=$DBNAME --dbuser=$DBUSER --dbpass=$DBPASS | |
mysqladmin -u $DBUSER -p$DBPASS create $DBNAME | |
wp core install --url=$WP_URL --title="Your Newly WordPress" --admin_email=$ADMIN_EMAIL --admin_password=$ADMIN_PASSWORD | |
(sudo sh -c "echo '\n<VirtualHost *:80> | |
DocumentRoot "$WP_DIR" | |
ServerName $WP_URL | |
ServerAlias $WP_URL | |
<Directory "$WP_DIR"> | |
Options FollowSymLinks | |
AllowOverride All | |
</Directory> | |
</VirtualHost> | |
' >> /etc/apache2/extra/httpd-vhosts.conf") | |
(sudo sh -c "apachectl restart") | |
(sudo sh -c "echo -e '\n127.0.0.1 $WP_URL' >> /etc/hosts" ) | |
# install the _s theme | |
curl -d "underscoresme_generate=1&underscoresme_name=$THEME_SLUG&underscoresme_slug=&underscoresme_author=&underscoresme_author_uri=&underscoresme_description=&underscoresme_generate_submit=Generate" http://underscores.me > me.zip | |
wp theme install me.zip | |
wp theme activate $THEME_SLUG | |
for p in $PLUGINS | |
do | |
echo "Installing $p plugin..." | |
wp plugin install $p | |
done | |
for p in $PLUGINS | |
do | |
echo "Activating $p plugin..." | |
wp plugin activate $p | |
done |
We start with the wp core download
and wp core config
commands; they will download the latest WordPress version and create a config file based on the supplied parameters at the command line interface. After that, we issue the wp core install
command, which starts the installation (much like if you were visiting the page on a regular browser). Next, we add the vhost to Apache and restart it. After that, the vhost gets added to the system’s host file, and finally, we download and install our starter theme.
If you don’t immediately grasp how wp-cli it works, you might well be tempted to skip it. However, if you stick with it for a while and learn how to leverage it, you will find that it can save you a good few hours of work. This is just an example of how wp-cli can be useful.