wordpress-rest-curl/post.sh
2026-07-30 17:44:40 +02:00

132 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# WordPress posting script
# config:
EDITOR=vim
TRANSFORM=() # empty or a subset of: 'title' 'markdown'
USER="" # WP user to create the post
PASSWORD="" # application password generated for your WP user
SERVER="" # server hostname, optionally with subdirectories
STATUS="draft" # one of publish,future,draft,pending,private
CATEGORIES="" # comma separated integer IDs of categories
TAGS="" # comma separated integer IDs of tags
TMPFILE=/tmp/wordpress-post.txt # location of a temporary file with the post text
source ~/.config/wordpress-rest-curl/config.sh
# CLI options (override config):
# --edit POST_ID update an existing post instead of creating a new one
# --publish shortcut for STATUS=publish
# --file PATH use PATH as the post source instead of opening $EDITOR
POST_ID=""
SOURCE_FILE=""
while [[ $# -gt 0 ]]; do
case "$1" in
--edit)
POST_ID="$2"; shift 2 ;;
--publish)
STATUS="publish"; shift ;;
--file)
SOURCE_FILE="$2"; shift 2 ;;
*)
echo "Unknown argument: $1" >&2
exit 1 ;;
esac
done
# let the user provide the post content
if [[ -n "$SOURCE_FILE" ]]; then
cp "$SOURCE_FILE" "$TMPFILE" || exit 1
else
$EDITOR $TMPFILE || exit 1
fi
[[ -e $TMPFILE ]] || exit 1
# transformations
cp $TMPFILE $TMPFILE.trans
for T in "${TRANSFORM[@]}"; do
if [[ "$T" == "title" ]]; then
python3 >$TMPFILE.trans2 <<EOF
import re
with open('$TMPFILE.trans', 'r') as file:
title = ''
content = file.read()
matches = re.match('^#+ (.+)', content, flags=0)
if matches:
title = matches.group(1)
content = content[len(matches.group(0)) + 1:]
with open('$TMPFILE.title', 'w') as titlefile:
titlefile.write(title)
print(content)
EOF
TITLE=`cat $TMPFILE.title`
rm $TMPFILE.title
mv $TMPFILE.trans2 $TMPFILE.trans
elif [[ "$T" == "markdown" ]]; then
python3 >$TMPFILE.trans2 <<EOF
import markdown2
print(markdown2.markdown_path('$TMPFILE.trans'))
EOF
mv $TMPFILE.trans2 $TMPFILE.trans
fi
done
CONTENT=`cat $TMPFILE.trans`
rm $TMPFILE.trans
echo "--- START POST ---"
echo "$CONTENT"
echo "--- END POST ---"
echo "Title: $TITLE"
echo "User: $USER"
echo "Server: $SERVER"
echo "Status: $STATUS"
echo "Categories: $CATEGORIES"
echo "Tags: $TAGS"
if [[ -n "$POST_ID" ]]; then
echo "Editing post: $POST_ID"
fi
echo
read -p "Press enter to confirm..."
# build the JSON payload (categories/tags as real arrays, not comma strings)
CATEGORIES_JSON="[]"
if [[ -n "$CATEGORIES" ]]; then
CATEGORIES_JSON=$(echo "$CATEGORIES" | tr ',' '\n' | jq -R 'select(length > 0) | tonumber' | jq -s '.')
fi
TAGS_JSON="[]"
if [[ -n "$TAGS" ]]; then
TAGS_JSON=$(echo "$TAGS" | tr ',' '\n' | jq -R 'select(length > 0) | tonumber' | jq -s '.')
fi
PAYLOAD=$(jq -n \
--arg title "$TITLE" \
--arg content "$CONTENT" \
--arg status "$STATUS" \
--argjson categories "$CATEGORIES_JSON" \
--argjson tags "$TAGS_JSON" \
'{title: $title, content: $content, status: $status, categories: $categories, tags: $tags}')
BASE_URL="$SERVER"
[[ "$BASE_URL" != http://* && "$BASE_URL" != https://* ]] && BASE_URL="https://$BASE_URL"
URL="$BASE_URL/wp-json/wp/v2/posts/"
[[ -n "$POST_ID" ]] && URL="$BASE_URL/wp-json/wp/v2/posts/$POST_ID"
# push the post!
RESPONSE=$(curl --silent --show-error --user "$USER:$PASSWORD" -X POST \
-H "Content-Type: application/json" \
--data "$PAYLOAD" \
-w '\n%{http_code}' \
"$URL")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [[ "$HTTP_CODE" -lt 200 || "$HTTP_CODE" -ge 300 ]]; then
echo "Error: WordPress API returned HTTP $HTTP_CODE" >&2
echo "$BODY" >&2
exit 1
fi
echo "$BODY"
# backup the posted data (temporarily until it is auto-removed)
mv $TMPFILE $TMPFILE.posted