From 397fdb1eded8ec3db75134983b1118a72383fc9a Mon Sep 17 00:00:00 2001 From: Matt Way Date: Sun, 11 Aug 2024 21:51:57 +1000 Subject: [PATCH] Inital script for uploading media to wordpress --- upload-media.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 upload-media.sh diff --git a/upload-media.sh b/upload-media.sh new file mode 100644 index 0000000..5bc26f8 --- /dev/null +++ b/upload-media.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Tries to upload media to wordpress and stores a json file with the ID and url of the media. + +# This script uses the jq, and wp commands, make sure they are installed before running this script. + +# Additionally, make sure the wp-cli/restful package is installed in the wp command (via "wp package install wp-cli/restful") + +# Controls the flags that are passed to every usage of the wp command. +WP_FLAGS="--allow-root --path=/var/www/html" + +media_path="$1" + +if test -f "$media_path"; then + if test -f "$media_path.json"; then + echo "Found $media_path.json, skipping uploading media." + else + echo "Could not find $media_path.json, uploading media!" + id=$(wp media import "$media_path" --porcelain $WP_FLAGS) + url=$(wp rest attachment get "$id" --field=source_url $WP_FLAGS) + jq -n --arg id $id --arg url "$url" '{"id": $id, "url": $url}' > "$media_path.json" + cat "$media_path.json" + fi +else + echo "Could not find $media_path" +fi