WordPress API N00b Seeks Enlightenment

Daniel Smith
4 min readJun 11, 2018

--

[I’ve been a developer for… well, decades. I’m new to the WordPress API though, and.. I have questions…]

I just put out a Proof Of Concept yesterday, where I decided to do a Vue.js front end to headless WordPress. If you want to see that, it is over at:

So, my goal was to come up to speed on using the WP REST API. I wanted to do something interesting and non-trivial:

  • use Vue.js as a front end
  • upload media by file select, drag & drop, or snap & paste
  • create posts automatically
  • do this with as few changes to WP as possible: no themes, no php, no new plugins. You know, Headless, not Hybrid

The Articles Out There

When I went to research how others have worked with API, I found that 95% of the articles took one of two approaches:

  • use the API, but only to retrieve existing data
  • perhaps post something, but wrap the code up in a theme, and use some php.

Really? That’s It?

Boring. Come on, people :) There are some pretty legit reasons why someone would want to have a front end without any WP install, calling to WP on another server. Perhaps they dont want to mess with a theme that the end users are seeing. Perhaps there is some tight control on the WP side, and the owners only want to make the bare minimum changes to accommodate incoming REST calls.

And perhaps someone thinks they can do a better Vue, React, or Angular front end for certain types of publishing.

I hope that I merely overlooked some great articles out there that show how to do a completely decoupled publishing front end.

WP Config Problems I Ran Into…

  • authentication — my first thought was to use Basic Authentication. For whatever reason, this didn’t just work, and lead to some searching, and Stack Overflowing, and.. why don’t I just use JWT? I love JWT. So I did.
  • JWT requires a plugin — my naive goal was not to touch the WordPress config at all. I punted on this and set up Simple JWT Authentication
  • .htaccess needs help — I had to add a few lines to set up CORS. Not too bad, but something to be aware of

Overall, this is not bad. The takeaway is that if you are going to use the API for something other that just consuming data, there are some WP server side config issues you’ll need to address.

Issues And Questions

Here is where I get into some issues I ran into when using the API in a completely decoupled/headless mode:

  • can’t just make one call to post

I could just explain that one thing and be done! But I will break it down.

To make a post, say, with media, and tags. you would need to do the following (now, remember: I am new at this. If I am missing something obvious, let me know):

  • upload an image, or sound file, or video… get the response, and hang on to the media ID
  • get the IDs of tags you want to use… so, that means you would need to fetch tags that match what the user typed in. If they came up with a new tag, then… wait, so.. you have to go and make the new tag(s), and get the IDs for those
  • same for categories…
  • ok, now, once you have done a roundtrip to upload media, and perhaps several more roundtrips to take care of all your tag IDs…now you can post.

This probably explains a lot as to why you dont see many articles that address creating posts with media and tags from Vue/React/Angular to Headless WP: it’s a long-winded process! I worked through the media + post portions, and I may iterate to tackle tags.

It is way more work than it should be. I would love it if some experienced WP people told me that I am wrong, and that I should read this or that article. The caveat there is: I am not interested in Hybrid/PHP/mess with themes mode.

Enlighten Me!

Batch It

What I wish the WP REST API supported? Batching things.

I want to make one call where I say:

  • here’s one or more files to upload
  • here are my tags — create them if they dont already exist
  • here is my post content
  • give me back the id and links to my post, media, tags, and so on

I could see two scenarios for that:

  1. allow for batching in the API itself
  2. or, have a plugin that can accept all the data at once, act as a go-between and deal with WP Core, and hand back a JSON object with the results of a new post

To sum up, I am excited to be able to create a standalone publishing POC for WP, but I find the process of dealing with the API to be pretty clunky. I am wondering if I am missing something, or if that is just the way it is.

--

--

Daniel Smith

codes/writes, LAMP / MEVN / MERN, loves film, music, and bantering - I am available for projects — javajoint [at the domain of] gmail [which is a dot] com