Initial commit.

This commit is contained in:
Dejvino 2020-10-06 22:28:37 +02:00
parent 7769e32b3f
commit 54723b3455
3 changed files with 72 additions and 1 deletions

View File

@ -1,3 +1,21 @@
# wordpress-rest-curl
CLI tool for submitting posts to WordPress through its REST API using curl.
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!

9
config.sh.template Executable file
View File

@ -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

44
post.sh Executable file
View File

@ -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