Did you know you could directly access all the media files in your WordPress library via the API?1 All I had to do was look and here’s the documentation on all sorts of nice options.
You can also restrict the query by file type with a URL like this option to get 10 most recent GIFs.
const url = 'https://bionicteaching.com/wp-json/wp/v2/media?mime_type=image/gif&per_page=10'
Now I could make a little page of GIFs like so.
See the Pen
WordPress Media Library JSON API by Tom (@twwoodward)
on CodePen.
1 Also know that anyone could access these files in a similar way if you haven’t taken action to lock things down. It’s also worth checking on this particular path even if you have locked down access in other ways.
GIFs for all my friends! Love this!
Genius! The script that keeps on giffing. Likely an option to get 10 random ones?
There’s no random sort parameter in the API by default. I did look. 🙂 Likely because stuff like rand are resource intensive. I think WP Engine blocked that PHP function entirely.
I could build one and I might end up doing that . . . (and I might have done it for posts in the past). If so, I’ll post it.
What’s this WP Engine you speak of? 😉
But what if your API call grabbed as many as you could get, or maybe you have Javascript come upo with a random “older than” date to grab (might not be an option). But if you could get data on maybe 50 or 100 gifs, you could just do the randomness in Javascript as array indices (?)
Definitely an option. We could create semi-random presentations by messing with date ranges and sort options with both being set as arrays to randomize between prior to the fetch. To play around more, maybe pick random numbers for sort order via flexbox order property or something. Duplicates wouldn’t matter. That’d put in a pretty good amount of the kind of randomness we’d want.