From 54723b3455113394e66079b37072992e9c565fe2 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Tue, 6 Oct 2020 22:28:37 +0200 Subject: [PATCH] Initial commit. --- README.md | 20 +++++++++++++++++++- config.sh.template | 9 +++++++++ post.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100755 config.sh.template create mode 100755 post.sh diff --git a/README.md b/README.md index 3aad37a..87f97c6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ # wordpress-rest-curl -CLI tool for submitting posts to WordPress through its REST API using curl. \ No newline at end of file +CLI tool for submitting posts to WordPress through its REST API using curl. + +## Install +### WordPress +Install [Application Passwords](https://wordpress.org/plugins/application-passwords/) plugin and follow its installation steps (i.e. create a new passowrd for your user). + +### Local shell +Make sure you have `curl` installed. + +Create a config file `~/.config/wordpress-rest-curl/config.sh` from the `config.sh.template` file. + +Copy the posting script to e.g. `/usr/local/bin/wordpress-post` so that you can run it from anywhere. + +## Usage +* Run the script +* Text editor opens. Type your post, save it. +* Confirm the displayed post by pressing enter. +* Done! + diff --git a/config.sh.template b/config.sh.template new file mode 100755 index 0000000..fe25069 --- /dev/null +++ b/config.sh.template @@ -0,0 +1,9 @@ +#!/bin/bash +EDITOR=vim +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 + diff --git a/post.sh b/post.sh new file mode 100755 index 0000000..98b070e --- /dev/null +++ b/post.sh @@ -0,0 +1,44 @@ +#!/bin/bash +# WordPress posting script + +# config: +EDITOR=vim +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 + +# let the user create the post +$EDITOR $TMPFILE || exit 1 + +# load the file if it exists +[[ -e $TMPFILE ]] || exit 1 +CONTENT=`cat $TMPFILE` + +echo "--- START POST ---" +echo $CONTENT +echo "--- END POST ---" +echo "User: $USER" +echo "Server: $SERVER" +echo "Status: $STATUS" +echo "Categories: $CATEGORIES" +echo "Tags: $TAGS" +echo +read -p "Press enter to confirm..." + +# push the post! +curl --user "$USER:$PASSWORD" -X POST \ + --data-urlencode "content=$CONTENT" \ + --data-urlencode "status=$STATUS" \ + --data-urlencode "categories=$CATEGORIES" \ + --data-urlencode "tags=$TAGS" \ + "https://$SERVER/wp-json/wp/v2/posts/" || exit 1 + +# backup the posted data (temporarily until it is auto-removed) +mv $TMPFILE $TMPFILE.posted +