Action for WordPress publishing


#1

Update: created a Workflow app workflow to do it. Will now be figuring out how to get it to take the first Header 1-formatted line as the post title if I can.

Greetings,

I’ve followed the instructions for using the WordPress action, but cannot get it to work. Upon running it, it launches a URL that clearly is expecting the slug for the post in question but it shows up as ‘undefined’.

Is there another action that might do the trick? I could use the Share sheet and select WordPress, but this pastes the draft as quoted text, which is easy to manually fix but is annoying.


#2

Hi Chris,

I had a similar error and received the kind advice below from Colin (the Action’s author).

After installing the “Basic Authentication handler” plugin in my WordPress, I could post.

Hopefully it helps you troubleshoot.

+++
The 401 response is unauthorised which means you’re not getting logged in so the post can’t be created - which is why you get p=undefined in the URL.

Have you installed the “Basic Authentication handler” plugin to allow the action to connect? https://github.com/WP-API/Basic-Auth

The action won’t work without it. If you do have it installed it could be an issue with your setup not allowing basic auth. Have a look at the thread below on GitHub for ideas on how to allow this if blocked. It may need you to add something to your .htaccess file.


+++

Ivan


#3

I did have it installed but still couldn’t get it to work. Also, I had read something about the plug-in not being available in the library on WordPress due to its storing of credential info, which gave me pause.

I have it almost working using a Workflow app workflow, can get it to retrieve the first line as the Name object for the post title, but unfortunately haven’t yet figured out how to parse the ‘body’ portion (sans first line) and pass that to the WordPress post as input.


#4

[[title]] vs [[body]] tags of the active draft don’t meet the need ?

(with Draft.processTemplate(String) -> String)


#5

They likely will as soon as I take the time to write a script that will put each where they need to be for the WordPress post! Maybe they have a web service I can hook into.


#6

Sounds good.

In the meantime, I’m sharing the script which I modified from Colin’s here to allow the selection of post categories: https://actions.getdrafts.com/a/1BS

// Ask for a post category

const categoriesArray = [
{'id':'32','category':'Google Analytics'},
{'id':'33','category':'Google Merchandise Store'},
{'id':'34','category':'News'},
{'id':'13','category':'Pets'},
{'id':'14','category':'Photography'},
{'id':'12','category':'Travels'},
{'id':'11','category':'Wild Life'}
];

// prompt to select a category

var p = Prompt.create();
p.title = "Select posting category";
for (var cat of categoriesArray) {
p.addButton(cat.category);
}

if (p.show()) { // user made a selection
var postCategory = p.buttonPressed;

const result = categoriesArray.find( categories => categories.category === postCategory );

var postCategories = [result.id];

// set up the initial prompt for post status
var p = Prompt.create();
p.isCancellable = "true";
p.addButton("Publish");
p.addButton("Draft");

if (p.show()) {

if (p.buttonPressed == "Publish") {
  var status = "publish";
} else {
  var status = "draft";
}

console.log(p.buttonPressed);

// Error if empty

if (draft.content == "") {
  alert("The draft is empty!");
  throw new Error("The draft is empty!");
}

// define Rest API endpoint

var endpoint = credential.getValue("host")+"/wp-json/wp/v2/posts";

var postContent = draft.processTemplate("[[body]]");


// create and post HTTP request

var http = HTTP.create();
var response = http.request({
  "url": endpoint,
  "method": "POST",
  "encoding": "form",
  "data": {
    "title": draft.title,
    "content": postContent,
    "categories": postCategories,
    "status": status,
    "format": "standard"
  },
  "headers": {
    'Authorization': 'Basic '+encodedString
  }
});

console.log("Response: " + response.statusCode);

// read the API response, get the post ID of the created item then view the post

var responseStr = JSON.parse(response.responseText);
var id = responseStr.id;

var url = credential.getValue("host")+"/?p="+id+"&preview=true";

app.openURL(url,false);

} else {
  console.log("Cancelled");
}

}

else { // user cancelled categories prompt
console.log("Cancelled");
}