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.
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.
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 […]
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 […]
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.
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.
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.