WordPress CLI – Quicksite creation script

By June 3, 2015 Uncategorized No Comments

A simple script I’ve written to quickly spin up WordPress sites, some folders and configurations might vary but it should be an easy swap. This requires WordPress CLI. It’s also using Git to grab a Zurb Foundation based theme called Reverie. The spacing was removed by WordPress, so the code all appears to not be indented properly.

I would create an alias for the script so you can type: youralias yoursitename, otherwise you’d run the script with your sitename after it.



#!/bin/bash

#!/usr/bin/expect

# Script to add a new site vhost for apache, create

# a directory for the site's files, and install

# wordpress, themes and plugins

# This is for the */var/www/* directory

rootuser="user"

rootpass="password"

sitename="$1"

serveralias=""

printf "Are you sure you want to install WordPress? \n"

read answer

if echo "$answer" | grep -iq "^y" ;then

printf "Creating the following database: \n"

export cut="`echo "${sitename}" | cut -c -10`"

printf "Database Name: ${cut} \n"

printf "Database User: ${cut}_user \n"

printf "Database Password: ${cut}_password \n"

RESULT=`mysql -u $rootuser -p$rootpass --skip-column-names -e "SHOW DATABASES LIKE '${cut}'"`

if [ "$RESULT" == "${cut}" ];then

echo "Database already exist, please choose a different name."

echo "Please choose a different site name."

end

else

echo "Database does not exist."

mysql -u $rootuser -p$rootpass -e "create database ${cut}; GRANT ALL PRIVILEGES ON ${cut}.* TO [email protected] IDENTIFIED BY '${cut}_password'"

echo "Database & User Created."

fi

sed "s/domain/$sitename/g" /var/sitefiles/vhostempty > /etc/apache2/sites-available/$sitename.com.conf

cd /var/www/

mkdir $sitename

cd /var/www/$sitename/

echo "Ok, grabbing WordPress."

wp core download --allow-root

wp core config --dbname="$cut" --dbuser="$cut"_user --dbpass="$cut"[email protected] --allow-root

wp core install --admin_user=admin --admin_password=password [email protected] --title=$sitename --url=$sitename.$serveralias.godigitaldev.com --allow-root

cd wp-content/themes/

git clone https://github.com/milohuang/reverie.git

mv reverie $sitename

printf "Install base plug-in set? \n"

read answer

if echo "$answer" | grep -iq "^y";then

cd /var/www/$sitename/

echo "Getting plug-ins."

wp plugin install disable-comments --allow-root

wp plugin install anti-spam --allow-root

wp plugin install google-analytics-for-wordpress --allow-root

wp plugin install image-widget --allow-root

wp plugin install wordpress-seo --allow-root

wp plugin install wp-google-maps --allow-root

wp plugin install adminimize --allow-root

else echo "Have it your way."

fi

printf "Is this e-commerce? \n"

read answer

if echo "$answer" | grep -iq "^y";then

cd /var/www/$sitename/

echo "Grabbing WooCommerce just for you."

wp plugin install woocommerce

else echo "Alrighty then."

fi

printf "So, do you need custom post types/fields? \n"

read answer

if echo "$answer" | grep -iq "^y";then

echo "Grabbing those plugins."

cd /var/www/$sitename/

wp plugin install advanced-custom-fields --allow-root

wp plugin install custom-post-type-ui --allow-root

fi

printf "Do you want to install licensed plugins (Gravity Forms, Visual Composer, etc)? \n"

read answer

if echo "$answer" | grep -iq "^y";then

echo "You have excellent taste."

cd /var/www/$sitename/

wget https://coreblend.blob.core.windows.net/your/archive.tar.gz

tar -xzf /var/www/$sitename/archive.tar.gz

rm -f /var/www/$sitename/archive.tar.gz

else echo "Ok."

fi

ln -s /var/www/$sitename /var/www/html/clients/$sitename

cd ~

a2ensite $sitename.com

service apache2 reload

else echo "Ok, bye."

fi

end

Simple, but significant Let's get started.