Skip to content

Instantly share code, notes, and snippets.

@Blake-C
Last active November 23, 2015 05:39
Show Gist options
  • Save Blake-C/bca63878482d0af2eb32 to your computer and use it in GitHub Desktop.
Save Blake-C/bca63878482d0af2eb32 to your computer and use it in GitHub Desktop.
WP-CLI Wordpress Build
#!/bin/bash -e
# Resources
# http://www.ltconsulting.co.uk/getting-wp-cli-work-mamp/
# http://www.ltconsulting.co.uk/automated-wordpress-installation-with-bash-wp-cli/
wp-init () {
echo ""
echo "================================================================="
echo "WP CLI WordPress Installer"
echo "================================================================="
echo ""
working_dir="${PWD}"
if [ -d "$working_dir" ]; then
if [ ! "$(ls -A "$working_dir" | wc -l)" -ne 0 ]; then
# Accept user input for the databse name
echo "Database Name: "
read -e dbname
echo ""
# Accept the name of our website
echo "Site Name: "
read -e sitename
echo ""
# Accept user input for the databse name
echo "Wordpress User: "
read -e wpuser
echo ""
# Accept user input for the databse name
echo "Wordpress User Email Address: "
read -e wp_admin_mail
echo ""
# add a simple yes/no confirmation before we proceed
echo "Make sure your database and server application is turned on. MAMP, XAMP, WAMP, AMP..."
echo ""
echo "Run Install? (y/n)"
read -e run
echo ""
# if the user didn"t say no, then go ahead an install
if [ "$run" == n ] ; then
exit
else
# Make sure MAMP Pro is open
if [ -d "/Applications/MAMP/" ]; then
open -a "mamp pro"
fi
# Download WordPress
wp core download
# Custom git init function that adds base .gitignore and sets initial commit
# Look at https://gist.github.com/Blake-C/013f73cb31b549c0bc63 for function
echo ""
echo "Committing the base build"
echo ""
git-init
# Create the wp-config file with our standard setup
wp core config --dbname=$dbname --dbuser=root --dbpass=root
# Parse the current directory name
currentdirectory=${PWD##*/}
# Generate random 12 character password
password=$(LC_CTYPE=C tr -dc A-Za-z0-9_\!\@\#\$\%\^\&\*\(\)-+= < /dev/urandom | head -c 12)
# Copy password to clipboard
echo $password | pbcopy
# Create database, and install WordPress
wp db create
wp core install --url="$currentdirectory:8888" --title="$sitename" --admin_user="$wpuser" --admin_password="$password" --admin_email="$wp_admin_mail"
# show only 6 posts on an archive page, remove default tagline
wp option update posts_per_page 6
wp option update blogdescription ""
# Delete sample page, and create homepage
wp post delete $(wp post list --post_type=page --posts_per_page=1 --post_status=publish --pagename="sample-page" --field=ID --format=ids)
wp post create --post_type=page --post_title=Home --post_status=publish --post_author=$(wp user get $wpuser --field=ID --format=ids)
# Set homepage as front page
wp option update show_on_front "page"
# Set homepage to be the new page
wp option update page_on_front $(wp post list --post_type=page --post_status=publish --posts_per_page=1 --pagename=home --field=ID --format=ids)
# Set pretty urls
wp rewrite structure "/%postname%/" --hard
wp rewrite flush --hard
# Install and active plugins, then remove default plugins
# Get rid of default plugins, probably not a great way to go about it
rm -rf wp-content/plugins
mkdir wp-content/plugins
touch wp-content/plugins/index.php
wp plugin install advanced-custom-fields --activate
wp plugin install wordpress-seo --activate
wp plugin install redirection --activate
wp plugin list --status=inactive --field=name --format=csv | xargs wp plugin uninstall
# Delete sample posts and comments
wp post list --field=ID --format=csv | xargs wp post delete
wp comment list --field=ID --format=csv | xargs wp comment delete
# Commit plugins
git add .
git commit -m "[plugins] Added base plugins"
#Setup main navigation
wp menu create "Main Navigation"
# add pages to navigation
export IFS=" "
for pageid in $(wp post list --order="ASC" --orderby="date" --post_type=page --post_status=publish --posts_per_page=-1 --field=ID --format=ids); do
wp menu item add-post main-navigation $pageid
done
# Install and active base theme, then remove default themes
wp theme install https://github.com/Blake-C/Wordpress-Theme/archive/master.zip
mv wp-content/themes/Wordpress-Theme-master wp-content/themes/$currentdirectory
if [ -d "wp-content/themes/$currentdirectory/" ]; then
# Move to theme directory
cd wp-content/themes/$currentdirectory
# Set working theme
sh set-theme.sh
# Check if bower exists then install bower components to start theme development
if type "bower" 2>/dev/null; then
bower install
else
echo ""
echo "Bower is not installed on your system, use either CodeKit or another tool to install bower components."
echo ""
fi
# Open Theme in Codekit
if [ -d "/Applications/CodeKit.app/" ]; then
open . -a "codekit"
fi
# Open Theme Sublime Text
if [ -d "/Applications/Sublime Text.app/" ]; then
open . -a "Sublime Text"
fi
# Open Theme Sublime Text
if [ -d "/Applications/Transmit.app/" ]; then
open . -a "Transmit.app"
fi
# Move back to root of project
cd ../../../
# Open Project in Tower or SourceTree
if [ -d "/Applications/Tower.app/" ]; then
open . -a "Tower"
elif [ -d "/Applications/SourceTree.app/" ]; then
open . -a "SourceTree"
else
echo ""
echo "No git application found, add the project manually or use command line."
echo ""
fi
# Activate default theme
wp theme activate $currentdirectory
# Uninstall inactive themes
wp theme list --status=inactive --field=name --format=csv | xargs wp theme uninstall
# Commit theme
git add .
git commit -m "[theme] Added base theme & removed wp themes"
# Optimization
mv wp-content/themes/$currentdirectory/sample.htaccess ./
rm .htaccess
mv sample.htaccess .htaccess
# Commit optimization
echo ""
echo "Committing optimizations"
echo ""
git add .
git commit -m "[htacces] Committing optimizations"
# assign navigaiton to primary location
wp menu location assign main-navigation primary
fi
echo ""
echo "================================================================="
echo ""
echo "Installation is complete. Your username/password is listed below."
echo ""
echo "Username: $wpuser"
echo "Password: $password"
echo ""
echo "================================================================="
# Open the new website with Google Chrome or Firefox if Chrome is not installed.
if [ -d "/Applications/Google Chrome.app" ]; then
open -a "/Applications/Google Chrome.app" "http://$currentdirectory:8888/wp-login.php" "http://realfavicongenerator.net/"
elif [ -d "/Applications/Firefox.app" ]; then
open -a "/Applications/Firefox.app" "http://$currentdirectory:8888/wp-login.php" "http://realfavicongenerator.net/"
fi
fi
else
echo "There are files in this directory. Run this script in an empty directory."
fi
else
echo "This is not a directory. This might be a symlink, run this command in a directory."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment