Developing software in the Real World

Embedding Notist slides

This site uses WordPress under the hood as I find the flexibility that a good CMS provides quite useful. For the talks section, I use a custom post type so that I can set additional properties on the post and customise the display. With my usual lack of imagination, my custom post type is called talk.

When Notist was released, I’ve been uploading the PDFs for my presentations there so that I have a nicely rendered online version. If I remember, I also add relevant tweets to the Notes presentation too. However, as I like to have a page on this site for each talk, I then embed the Notist slides back here.

I used to have a rather hard-coded solution that only supported 16:9 aspect ratio presentations, but as a friend suggested that I should write it down, I reached out to Drew McLellan to see how I could improve it. Rather helpfully, he pointed me at the right way to do it!

oEmbed

Drew informed me that Notist uses oEmbed. The oEmbed website describes it as:

oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly.

This is exactly what we need & for Notist, the API call is:

This will then return a payload along these lines:

There’s three pieces of information we need: html, cache_age & the aspect ratio (width/height).

Implementation in WordPress

To implement this within my custom post type, I registered these post meta data fields:

  • talk_notist_code
  • talk_notist_embed_html
  • talk_notist_embed_expires
  • talk_notist_embed_aspect

In my talk post type’s admin page, I provide an input field for talk_notist_code as this is the unique code from Notist and I need to enter it on my page as the link between the talk page on my site and the presentaiton on Notist.

The others (talk_notist_embed_html, talk_notist_embed_expires & talk_notist_embed_aspect) hold the embedded HTML to render, the time when we need to refetch that HTML and the aspect ratio of the presentation respectively. I hide them in the admin UI and never think about them.

Fetching the HTML

To fetch the HTML code to embed, I wrote this function:

This function grabs the JSON from Notist’s oEmbed API and then extracts the HTML and cache_age. We add the cache_age to the output of time() so that we know when to refetch. The aspect ratio can be calculated by dividing the thumbnail’s width and height.

One nice thing about PHP is that file_get_contents() is a really easy way to do a GET request on an API. I’ve put in some minimal error checking to hopefully ensure that it won’t fall over if the Notist API is down or returning something expected. I’ll add error logs and alerting at some point too. Note that as we need to return multiple pieces of information, we return an array with the information.

Store the HTML and timeout

We don’t want to have to fetch this HTML every time the page is loaded on our site, so we store it into WordPress’s post_meta and only fetch it if we don’t have it. As I’m a little lazy, I do this in content-talk.php:

We retrieve notist_embed_html, notist_embed_expires & notist_embed_aspect from the WordPress post_meta and if there is no HTML or it has expired, we fetch them using our shiny new get_notist_embed_html(). Assuming we actually found some HTML, we then update the post_meta fields with the new HTML code, the updated expiry time and the aspect ratio, ready for the next page load.

Render the HTML

We can now render the HTML that will embed the presentation in our page. As we’re not totally trusting, we put the HTML that we retrieved in a sandboxed iframe for security. Who knows what is in the JS file that Notist gives us?!

We need a div and an iframe in order to make our embedded presentation responsive. The containing div needs a padding-top that is related to the aspect ratio (75% for 4:3, 56.25% for 16:9), which is why we calculated and stored it earlier. My HTML/CSS isn’t the strongest, so maybe there’s an easier way?

You can see it in action on the Capturing Light talk page.

That’s it

With this small bit of code, not only can I embed Notist slides on my talk pages, but as and when Notist improves their slide player, I’ll pick up the improvements automatically. With the use of a cache, I also avoid too many API calls too.

There’s some WordPress specific code here, but this approach can be generalised to any website. It’d be nice if Notist supplied a WordPress plugin, but until then, this works for me.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *