Indieweb replacements

With a view to replacing Workflow with Drafts (at least as far as is currently possible) I decided to look at posting #indieweb like and replies directly from the app itself.

I was wondering how to pull in the post title using JavaScript but then realised this would just be duplicating effort as my Likes and Replies plugin already does the heavy lifting. All I would need to do was pass the URL as part of the HTTP request so it could be used as the relevant custom field.

The catch, however, is that custom fields are not exposed via the API by default.

Luckily, we can extend the API response and expose extra fields.

Hooking into rest_api_init and using the register_rest_field method you can add a field and specify the callback functions for retrieving or updating that field via the API:

add_action( 'rest_api_init', 'slug_register_like' );
function slug_register_like() {
  register_rest_field( 'post',
    'Liked',
    array(
      'get_callback'    => 'slug_get_like',
      'update_callback' => 'slug_update_like',
      'schema'          => null,
    )
  );
}

The above sets up the "Liked" custom field for items of type "post" then defines the functions used to retrieve or update. I've not done anything special, just taken it directly from the API Handbook.

This means that I can update the post data as seen before with the custom field, where url is the web page to be liked or replied to:

"data": {
  "title": draft.title,
  "content": draft.content,
  "status": "draft",
  "format": "status",
  "comment_status": "open",
  "Liked": url
},

I've then created new actions in Drafts which take the URL from the clipboard, or prompt for one if the clipboard doesn't hold one, and submit a post as before. The only difference is that I have to update the post after submission for the Likes and Replies plugin to trigger.

Performing likes or replies using Workflow meant that I was typing into a small, plain text box. The new aproach means that I can type what I want to say in advance, with the benefits or Markdown support, and then trigger the process.

Mentions

  • Colin Walker