Skip to content

Instantly share code, notes, and snippets.

@terraboops
Last active June 14, 2016 14:22
Show Gist options
  • Save terraboops/75580b099c7dcae198cac404f8b8fa06 to your computer and use it in GitHub Desktop.
Save terraboops/75580b099c7dcae198cac404f8b8fa06 to your computer and use it in GitHub Desktop.
Generate Static HTML from a WP site
#!/bin/bash
echo "START"
OUTPUT_DIR="$1"
# Validate Output Dir
if [ -z "${OUTPUT_DIR}" ]; then
echo "OUTPUT_DIR is unset: [${OUTPUT_DIR}]"
exit 1
fi
URL="staging.my-awesome-site.com"
echo "[BUILD START: $(date)]"
# Archive site with wget
wget --quiet --mirror -P "${OUTPUT_DIR}" -nH -np -p -k -E "${URL}"
echo "[STATUS] wget completed"
# wget -k can break certain special characters used in HTML5 data attributes -- fix with Perl regex!
# Validate wget return code
WGET_RETURN=$?
if [ ${WGET_RETURN} -ne 0 ] && [ ${WGET_RETURN} -ne 8 ]; then
case ${WGET_RETURN} in
1)
echo "Error: Generic error code."
;;
2)
echo "Error: Parse error—for instance, when parsing command-line options, the ‘.wgetrc’ or ‘.netrc’..."
;;
3)
echo "Error: File I/O error."
;;
4)
echo "Error: Network failure."
;;
5)
echo "Error: SSL verification failure."
;;
6)
echo "Error: Username/password authentication failure."
;;
7)
echo "Error: Protocol errors."
;;
esac
echo "Failed to download archive of site: ${URL}"
exit 1
elif [ ${WGET_RETURN} -eq 8 ]; then
# This means a URL returned an error status, like 500 or 404.
# Nothing to worry about usually.
echo "Archive created successfully, with minor server errors: (nothing to worry about)"
else
echo "Archive created successfully:"
fi
#Pretty Print Archive Results
tree "${OUTPUT_DIR}"
echo "[BUILD FINISH: $(date)]"

Dependencies

  • bash
  • wget

Usage Instructions

Download the script and ensure it has correct permissions to be executed by the appropriate user. Modify the URL value in the script to point at your WP install.

Then run: ./build.sh my-static-folder

This will create a directory named "my-static-folder" which you can verify and deploy. I've found that wget can sometimes mangle certain special characters, escaping them as HTML entities. You can implement some perl regex on Line 19 of the script to resolve any issues like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment