I’ve got a lot of catching up to do with Tom Woodward on building sites that draw content from WordPress ones via the API but I’ve got reason and interest to start.

This has been a back of the mind idea for while for my sites that rely on random images drawn from the flickr API, e.g. pechaflickr and Five Card Flickr stories. They have been running quite fine for years, but a commons concern question is about the chance of one of the random images that come into play being inappropriate (like a photo of beets).

Many people might be interesting in running these kinds of sites from an image collection they control. The back brain engines have been mulling how it might work from using the WordPress API to get the featured images from photos in a site running my TRU Collector SPLOT theme.

I’ve only done a tiny bit of WordPress API tinkering, making a goofy site for the front end of arganee.world that repeatedly loaded some random text pulled via WordPress API from posts on the site.

Rather than pile on more verbiage, I am testing pulling stuff from the SPLOT Collector demo site, what I really need are, at a minimum, the featured images for each post.

What I was able to do, with a bit of code banging, was a custom API call to pull a given number of images (the last number on the URL is a parameter, returning an array of the title of the post, the link to the post, and the link to the thumbnail image, e.g. gimme 8

http://splot.ca/collector/wp-json/splotcollector/v1/randy/8, resulting ins returns like:

Here is the test bit of all the working pieces, running in codepen

See the Pen Pechasplot by Alan Levine (@cogdog) on CodePen.

Like I said, duct tape and crude.

So as it turns out, via the built in API there’s not a means to query for a random post, like one can do in WordPress via a WP_Query. So for what I wanted, it meant digging into adding my own custom endpoint to do the task.

Here is the code running on the site, added to functions.php that sets up the custom API call and handles what is sent back

It’s a start. But I can see with some connection to the existing scripts, to see a version of pechaflickr that would draw images from a SPLOT.


Featured Image: File:Duct-tape Moving Van.jpg Wikimedia Commons image shared into the public domain as the work of a US Government employee

If this kind of stuff has any value, please support me monthly on Patreon or a one time PayPal kibble toss
Profile Picture for Alan Levine aka CogDog
An early 90s builder of the web and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person.

Comments

Leave a Reply

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