#!/bin/bash
set -euo pipefail
green() { echo -e "\033[32m$1\033[0m"; }
red() { echo -e "\033[31m$1\033[0m"; }
yellow() { echo -e "\033[33m$1\033[0m"; }
bold() { echo -e "\033[1m$1\033[0m"; }
WP_TITLE=""
WP_ADMIN_USER=""
WP_ADMIN_PASSWORD=""
WP_ADMIN_EMAIL=""
usage() {
echo "Usage: $0 [-t
] [-u ] [-p ] [-e ]"
echo " -t : Set WordPress site title"
echo " -u : Set admin username"
echo " -p : Set admin password"
echo " -e : Set admin email"
echo " -h, --help : Display this help message"
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
-t) WP_TITLE="$2"; shift 2 ;;
-u) WP_ADMIN_USER="$2"; shift 2 ;;
-p) WP_ADMIN_PASSWORD="$2"; shift 2 ;;
-e) WP_ADMIN_EMAIL="$2"; shift 2 ;;
-h | --help) usage ;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
prompt_for_value() {
local var_name=$1
local prompt_text=$2
if [[ -z "${!var_name}" ]]; then
read -rp "$prompt_text: " value < /dev/tty
printf -v "$var_name" '%s' "$value"
fi
}
prompt_for_value WP_TITLE "Enter WordPress site title"
prompt_for_value WP_ADMIN_USER "Enter admin username"
prompt_for_value WP_ADMIN_PASSWORD "Enter admin password"
prompt_for_value WP_ADMIN_EMAIL "Enter admin email"
while ! echo "$WP_ADMIN_EMAIL" | grep -qE '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'; do
red "Invalid email address. Please provide a valid email."
WP_ADMIN_EMAIL=""
prompt_for_value WP_ADMIN_EMAIL "Enter admin email"
done
for cmd in wp mysql herd; do
if ! command -v "$cmd" &>/dev/null; then
red "$cmd is not installed. Please install it first."
exit 1
fi
done
DIRS=()
while IFS= read -r line; do
DIRS+=("$line")
done < <(herd paths | jq -r '.[]')
if [[ ${#DIRS[@]} -eq 0 ]]; then
red "No directories found from 'herd paths'. Please check your Herd configuration."
exit 1
fi
echo "Available directories:"
for i in "${!DIRS[@]}"; do
echo "$((i + 1))) ${DIRS[$i]}"
done
read -rp "Enter the number of the directory you want to use: " DIR_CHOICE < /dev/tty
if [[ ! "$DIR_CHOICE" =~ ^[0-9]+$ ]] || ((DIR_CHOICE < 1 || DIR_CHOICE > ${#DIRS[@]})); then
red "Invalid selection. Exiting."
exit 1
fi
CHOSEN_DIR="${DIRS[$((DIR_CHOICE - 1))]}"
yellow "You selected: $CHOSEN_DIR"
read -rp "Enter the new site folder name: " SITE_NAME < /dev/tty
if [[ -z "$SITE_NAME" ]]; then
red "Folder name cannot be empty. Exiting."
exit 1
fi
mkdir -p "$CHOSEN_DIR/$SITE_NAME" && cd "$CHOSEN_DIR/$SITE_NAME"
DB_USER="root"
DB_PASSWORD=""
DB_HOST="127.0.0.1"
WP_URL="$SITE_NAME.test"
DB_NAME="$SITE_NAME"
echo "Using the following values:"
echo "WordPress URL: $(green "$WP_URL")"
echo "Database Name: $(green "$DB_NAME")"
echo "WordPress Title: $(green "$WP_TITLE")"
echo "Admin Username: $(green "$WP_ADMIN_USER")"
echo "Admin Email: $(green "$WP_ADMIN_EMAIL")"
wp core download
wp config create --dbname="$DB_NAME" --dbuser="$DB_USER" --dbpass="$DB_PASSWORD" --dbhost="$DB_HOST"
wp db create
wp core install --url="$WP_URL" --title="$WP_TITLE" --admin_user="$WP_ADMIN_USER" --admin_password="$WP_ADMIN_PASSWORD" --admin_email="$WP_ADMIN_EMAIL" --skip-email
wp site empty --yes
wp search-replace http https
herd secure
wp rewrite structure '/%postname%'
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set SCRIPT_DEBUG true --raw
wp config set WP_ENVIRONMENT_TYPE local
wp config set WP_DEVELOPMENT_MODE all
echo
bold "$(green "Success: Your new WordPress site on Herd has been created!")"
echo "- Site URL: $(green "$WP_URL")"
echo "- Database Name: $(green "$DB_NAME")"
echo "- Admin username: $(green "$WP_ADMIN_USER")"
echo "- Admin password: $(green "$WP_ADMIN_PASSWORD")"
echo "- Admin email: $(green "$WP_ADMIN_EMAIL")"
echo
echo "Run '$(green "cd $CHOSEN_DIR/$SITE_NAME")' to navigate to the site directory."
read -rp "Would you like to open the site in your browser? (y/n) " -n 1 REPLY < /dev/tty
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [[ "$OSTYPE" == "darwin"* ]]; then
open "https://$WP_URL"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
xdg-open "https://$WP_URL"
else
echo "Unable to automatically open the browser on this operating system."
echo "Please visit https://$WP_URL manually."
fi
fi
echo
bold "$(green "Installation complete. Enjoy your new WordPress site!")"