Skip to content

Instantly share code, notes, and snippets.

@jordantrizz
Forked from jsonUK/wp-new.sh
Created April 17, 2023 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jordantrizz/28de7b86692d8825d470d9b77ae2ea70 to your computer and use it in GitHub Desktop.
Save jordantrizz/28de7b86692d8825d470d9b77ae2ea70 to your computer and use it in GitHub Desktop.
Script to create new WordPress setup, using WP-CLI
#!/bin/bash
#
# USE FOR LOCAL DEVELOPMENT ONLY!!!
#
EMAIL='your-email@gmail.com'
MYSQL=`which mysql`
echo -n "DB user/name for project (lowercase) [ENTER]:"
read DB
if [ -z "$DB" ]; then
echo "User must define a DB Name for your project."
exit;
fi
echo -n "Local URL eg. local.test.com [ENTER]:"
read URL
if [ -z "$URL" ]; then
echo "You must enter a valid url."
exit;
fi
echo -n "Theme slug [ENTER]:"
read THEME_SLUG
echo -n "Theme Name, like 'My Theme' [ENTER]:"
read THEME_NAME
#
# Start WP Installation
#
random-string() {
LENGTH=$1
if [ -z "$LENGTH" ]; then
LENGTH='20'
fi
cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w $LENGTH | head -n 1
}
# Generate random password (mac friendly)
PASS=`random-string 20`
PREFIX=''
# No prefix? Then just comment out the line below
PREFIX="--dbprefix=`random-string 6`_"
Q1="CREATE DATABASE IF NOT EXISTS $DB;"
Q2="GRANT ALL ON $DB.* TO '$DB'@'localhost' IDENTIFIED BY '$PASS';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q1}${Q2}${Q3}"
if [ ! -d "htdocs" ]; then
# Create directory for new WP
mkdir htdocs
fi
cd htdocs
echo "SUDO password for adding /etc/hosts entry";
# Add to our hosts file (saves adding it manually)
sudo -- sh -c -e "echo '127.0.0.1 $URL' >> /etc/hosts"
if [ -n "$(grep $URL /etc/hosts)" ]
then
echo "$URL was added succesfully \n $(grep $URL /etc/hosts)";
else
echo "Failed to Add $URL, Try again!";
fi
# Set up a login path for creating your dbs and users (must have global privileges)
# mysql_config_editor set --login-path=local --host=localhost --user=username --password
$MYSQL --login-path=local -e "$SQL"
echo "MYSQL user/db '$DB' created."
# Install latest WP
wp core download
wp core config --dbname="$DB" --dbuser="$DB" --dbpass="$PASS" $PREFIX
wp core install --url="$URL" --title='New WP Build' --admin_user='jason' --admin_password='local' --admin_email="$EMAIL" --skip-email
# Install & Active Plugins
# wp plugin install sucuri-scanner --activate
# wp plugin install wordpress-seo
wp plugin install autodescription
#wp plugin install google-sitemap-generator
wp plugin install advanced-custom-fields
wp plugin install regenerate-thumbnails
wp plugin install developer
# Install but leave inactive
# wp plugin install jetpack
#wp plugin install google-analytics-for-wordpress
# wp plugin install loginlockdown
# wp plugin install wp-retina-2x
wp plugin install wordpress-importer
# wp plugin install cookie-law-info
wp plugin install w3-total-cache
# Remove 'hello dolly' demo plugin
wp plugin delete hello
# Remove old default themes
wp theme delete twentyfifteen
wp theme delete twentythirteen
wp theme delete twentyfourteen
# Remove default posts, widgets, comments etc.
wp site empty --yes
wp scaffold _s "$THEME_SLUG" --theme_name="$THEME_NAME" --sassify
git clone https://github.com/roots/sage.git wp-content/themes/sage && rm -rf wp-content/themes/sage/.git
git clone https://github.com/roots/wp-password-bcrypt.git wp-content/mu-plugins/wp-password-bcrypt && rm -rf wp-content/mu-plugins/wp-password-bcrypt/.git
git clone https://github.com/roots/soil.git wp-content/plugins/soil && rm -rf wp-content/plugins/soil/.git
# Move the config a directory above
mv wp-config.php ../
# Make uploads writable
chmod -R 777 wp-content/uploads
# Is the directory a git repo? If not, create one
cd ..
if [ ! -d ".git" ]; then
git init .
fi
# Making a dumping folder
mkdir etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment