06

jQuery & the WordPress Rest API – 1st Image as Background

Ordinarily it’s pretty easy to get the featured image from the WordPress API. You just have to remember to add the _embed element like so https://bionicteaching.com/wp-json/wp/v2/posts?_embed But maybe there’s some reason you don’t want to set the featured image but want to get the first image from the body of the post. This chunk of so very uncool jQuery gets the JSON data and finds the first img src URL in the post body. I’ve gone over the basic pieces previously so here’s the portion that differs. It’s just regex searching for anything with the img src pattern and spitting back out the URL. See the Pen antonio – jquery demo by Tom (@twwoodward) on CodePen.

06

Weather API + Google Sheets

I have many little robot scripts busy saving data to Google Sheets. Here’s one I made based on a little comment from one of my kid’s teachers. It goes to a very kind URL that returns the data structure at the bottom of the page. http://api.wunderground.com/api/YOUR_API_KEY/conditions/q/VA/Richmond.json It’s a pretty nice starter script for anyone wanting to play with an API and Google Scripts. The full Google Script is below and I’ve tried to comment it up pretty thoroughly. I’ll also break it down below for those of you who want to play at home. Get the Data The first variable ‘url’ is the API endpoint we’re getting the data from. Where it says YOUR_API_KEY, you should put your API Key. I recall early on trying to do this stuff I’d get confused when people did different things to indicate that an API Key should go somewhere. People would put it in quotes or surrounded by asterisk or something else. I’d end up confused about what should go there because I had no baseline to judge this against. It’s fairly obvious now but I haven’t forgotten that feeling of basic confusion. Sometimes it helps to be very basic. Google has a nice UrlFetchApp class that you use in Google Script instead of fetch like you’d use in vanilla javascript land. Since […]

26

Add Custom Field to WordPress API & Filter

I found myself in a strange situation where I needed to know if a page had a child. Natively the WP API lets you know if a page has a parent but not the reverse. First I had to write a function that writes some data to a custom field if a page has a child. Then I started writing this post and realized I needed to do the reverse and now I’ve written a function that writes data to a parent when the child is created. Now that I had the data being written to the custom field, I needed to make that data visible in the WP REST API. Luckily, Jeff put up a snippet for that not too long ago. Now I also wanted to be able to return data based on the contents of the has_children field. This filter1 does that. Now a URL like the one below will give me pages with children but without parents. /wp-json/wp/v2/pages?_embed&per_page=30&has_children=1&parent=0 This will help me make a fairly large menu structure more manageable and I figured having something like this all in one place might help someone else. 1 I usually call things ‘chunks’ of code but I’m trying to improve my vocab.

A drawing of a small terrier dog jumping through a hoop held by a monkey.

Code as Poetry, Time as a Variable – Options in the Ether

Backstory Driving into work I was listening to NPR and they were interviewing Nikki Giovani a poet from Virginia Tech. In high school I was one of those people who really suffered reading the The Red Wheelbarrow and other non-rhyming poems. They irritated me in the same way people seem to be annoyed by White Paintings or 4’33”. In any case, in college I took lots of English classes. One of those classes was on poetry with Donna Hickey. The class selection was driven more by fitting my schedule and a vague notion that I might minor in English rather than any real interest in poetry. The first day of class she had everyone list their favorite poets. I don’t recall what people chose but I remember feeling like my choices of Shel Silverstein and Dr. Suess was not of the same category. In any case, I had a great deal of fun with the course and using poems as games and puzzles to think around and through. I later took a graduate course with Dr. Hickey1 in poetry and made my first digital liberal arts website around 2001 or 2002. It focused on breaking down various Richard Hugo poems and creating attempts at multimedia experiences.2 That stuff is all long gone from the UR website. I might have backup […]

29

Census Reporter API Exploration

We’re trying out a new site idea for the online digital sociology program. Once again, Matt came up with an idea that really got me excited and I started wandering around trying to figure out how to make it work. At some point I found CensusReporter.org. This is a pretty amazing site that is a Knight1 News Challenge-funded project. What we want to do is figure out roughly where the viewer/visitor is geographically and give them a glimpse of that area through some data. Nothing too deep, just a taste of the interesting ways technology, data, and visualization can blend together to do interesting things. We don’t want the exact address for a few reasons. One, we don’t want to be too creepy. Two, getting a rough location through the IP address doesn’t trigger the browser approval request that more exact location data requires. My goal is not to subvert the approving/denying of location data but I worry too many people will miss that approval prompt and we’re not keeping this data. Get Location To get local data I have to figure out where the person is. To do that, I’m just grabbing the IP address and getting a location bounced back. There are a few ways to do this. I went with freegeoip.net and the following function would give […]

13

YouTube Full Screen Player Page from Playlist

The request was that we do something interesting with the random screens we have scattered around our area. It seemed like it’d be easy to make a little full-page player for a YouTube playlist with some lightweight overlays to emphasize the connection with our department. Handling the videos displayed via a playlist makes it easy to add/remove content and the making the logo overlays part of the webpage let us keep things consistent across videos we didn’t control. Since we weren’t activating sound by default turning on captions by default was also a key need. The codepen example is here. The HTML is dead simple. We’ve got a wrapper div which lets us make it full screen, an ytplayer div for the videos, and two divs to do the logos. The javascript is pretty much a cut/paste from the YouTube API docs. There are a few changes to reference the playlist instead of a single video and some stuff around closed captions and that’s noted below. The main tweak here is to get the fullscreen video to behave. I use this pattern from CSS tricks. The other stuff just staples some divs over the lower corners to show the logos.

03

WordPress Plugin Health Dashboard (Early POC)

In trying to get a better handle on how we’re going to manage our plugins in the future, I found the API for the WordPress.org plugins data. Not like they were hiding it but I’d not seen it before.Consider me Columbus– absent the genocide, slavery, and other terrible things. Replacing *slug* in the following URL gives you a pretty robust javascript With that option, I could build a little Google Sheet/Scripts viewer that would look up information based on a list of plugin slugs. I could set conditional formatting to do various things for visual cues . . . I could even build a little mini-algorithm to evaluate different aspects and weight them towards a total plugin score. I’m debating whether it’s worth working into a more sophisticated plugin that will tell me how many sites the plugin is installed on, display that data etc. There are plugins like that out there but they die on large multisite installations.1 I’d also like some way to tie into the vulnerable plugin announcements. Here are the two little functions that look up and write the plugin’s health based on the slugs being written in column A and a header in row 1. 1 I have a functional, if crude, option that paginates through them in a way that doesn’t crash/die.

Archiving Slack Channels

Since we’re making channels in Slack via our project creation, it made sense to archive them when the project was completed. In projects (this particular post type) we have a custom field for the start date of the project and one for the end date of the project. Step one is to check on updates whether the post has the end-date field filled out. In my case, this is one of the legacy ACF fields that survived my great metadata purge. So checking it is done like so . . . The Slack archive API piece looks like this. And finally we run this function when projects are updated like so. We’re still experimenting with this workflow and archiving is a decent start. You can easily reactivate it and results still turn up in searches. It’s likely we’ll also rename it from p-whatever to z-whatever to get it out of the way.

Auto-Creating Slack Channels from WordPress

Image from page 279 of “The Ladies’ home journal” (1889) flickr photo by Internet Archive Book Images shared with no copyright restriction (Flickr Commons) In working through the project page a bit more, it seemed like it’d be more pleasant to start in WordPress and have our events echo outward in other services. One of those events is the creation of project specific Slack channels. In the past, I’d mainly listened for events . . . programmatically checking back every so often to see if something had occurred so I could do something else. In this case it made more sense to have one action directly precipitate another. These are the three functions that fire when we publish the custom-post-type Project.1 In any case, it’s a pretty instantaneous creation and invitation to the channels. 1 I don’t know how to punctuate that but it’s probably not hyphens.

List Public Slack Channels via API

Image from page 249 of “The development of the chick; an introduction to embryology” (1919) flickr photo by Internet Archive Book Images shared with no copyright restriction (Flickr Commons) I ended up doing this while pursuing some of the API integration stuff for our projects page. It doesn’t list the private pages and might be useful to someone. This was the byproduct of looking for a way to look up the ID for a particular channel which ended up looking like this.