Skip to content

Instantly share code, notes, and snippets.

@hzg
Created March 23, 2018 19:58
Show Gist options
  • Save hzg/188845f13a9c61b4b381eb3f09f49d09 to your computer and use it in GitHub Desktop.
Save hzg/188845f13a9c61b4b381eb3f09f49d09 to your computer and use it in GitHub Desktop.
Import local html to Wordpress Posts [Date in HTML Tag]
#!/bin/bash
#
# Requires Wp Cli - run in root of wp core install
##
echo "WP HTML File Importer - REQUIRES WP CLI"
read -p "Press enter to continue"
# CHANGE THIS YOUR DESIRED SRC
SRC="./articles/"
TEMPFILE="content_temp.txt"
# Initial Clear/Touch
> $TEMPFILE
# Months to Convert
MONTHS=("Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec")
# Loop Through all files
for HTMLFILES in $SRC/*.html; do
DATE=$(xmllint --html --xpath '//p[@class="date"]/text()' $HTMLFILES | tr -d '\n')
# echo $DATE
FILENAME=$(basename "$HTMLFILES")
YEAR="${DATE:9:4}"
CURMONTH="${DATE:0:3}"
DAY="${DATE:5:2}"
# Month Swap
MONTHCOUNT=0
for MONTH in "${MONTHS[@]}"; do
MONTHCOUNT=$((MONTHCOUNT + 1))
if [[ $CURMONTH == $MONTH ]]; then
# echo $MONTH
# echo $MONTHCOUNT
if [[ ${#MONTHCOUNT}<2 ]]; then
NEWMONTH=0$MONTHCOUNT
else
NEWMONTH=$MONTHCOUNT
fi
fi
done
POSTDATE=$YEAR\-$NEWMONTH\-$DAY
# echo $POSTDATE
H3=$(xmllint --html --xpath '//h3/text()' $HTMLFILES | tr -d '\n')
SYNOPSIS=$(xmllint --html --xpath '//div[@class="synopsis"]' $HTMLFILES | tr -d '\n')
# echo $H3
# echo $SYNOPSIS
echo "########"
echo $SYNOPSIS > $TEMPFILE
echo "Creating Post with Title $H3"
echo $POSTDATE
wp post create $TEMPFILE --post_title="$H3" --post_status='publish' --post_category='Press Release' --post_date=$POSTDATE
echo "########"
done
# Final Clear
rm $TEMPFILE
echo "-------------"
echo " FIN_ "
echo "-------------"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment