Enough of the old fart nostalgia and facebook whinging posts, it’s time to get back to some coding. I wanted to flex some WordPress muscles, and thought I was biting off on a small, one night bit of updates to the Daily Blank WordPress Theme (a generic thing that runs the DS106 Daily Create site).

Just to set up how the site works, it polls the twitter API once an hour for all tweets that are replies to @ds106dc (that’s why it’s required). Some info is stored locally (as a custom post type, for the WordPress junkies) for each one found, like it’s URL, as well as all hashtags found the tweet (saved as tags) most importantly the one that begins with tdc; these are unique to each Daily Create. For exmaple today’s tag is #tdc1597 and this makes it easy to find it’s URL — http://daily.ds106.us/tdc1597.

So… when you go to that one Daily Create, when the page load, it looks to find how many tweets have been saved with this tag, counts them, and then echoes them out to see using the oEmbed function on the tweet’s URL:

response

So we can see there are 9 responses so far. But getting this number means a database poll, and it would be nice to be able to show this number elsewhere (see the way it is shown on the front page for the current Daily Create)

daily-front

as well on the archives listing.

I had done something similar for the DS106 Assignment Bank Theme where each “assignment” / thing stores the number of responses, the number of associated tutorials, and number of views. It’s not too complicated, it means on the template page that displays a single Assignment of Daily Create, I am already running a query to find how many responses there are, so we simply store that in a piece of custom post metadata. And while we are doing that, we get a current value for another field that holds the number of views, we bump it one, and store this new value.

My approach for building this is framed with what I ready somewhere about WordPress (probably from Boone Gorges) that in terms of database/computational “costs”, reading data from the database is much cheaper a process than writing to it.

The work is done mostly in the single.php template.

We get the appropriate tag (store as post metadata), and then run a query to find out how many of the response post types exist with the same tag (e.g. how many tweeted responses for this one daily create)

The work then is passed to a common function dailyblank_update_meta() which we pass the database ID for the current post (Daily Create), and the number of responses found. This function does two things – it checks a post metadata field that tracks the number of visits, bumps it one, and updates the value. Then it compares the number it received for a number of responses, and updates the post meta data field if the new value is different.

Now we have information about the number of responses and number of visits associated with each Daily.

Bill Smith asked a legit question

But tracking unique users means keeping track of IP addresses, and while it may have more “meaning” is the overhead worth it? Who cares if someone wants to pump the view count by reloading the page? Occam’s Programming Razor applied here.

It works, but for the DS106 Daily Create, there are more than 200 previous posts that will not get their data setup unless someone happens to visit. I have added a new feature, that could use a nudge to backfill existing content.

I have a low tech approach to this, as I need to sometimes just for trying out new code, testing queries, etc. So the theme has a page template called page-debug.php that has nothing special in it. I use it for one time function calls or checking values. I write some code I need to run, put it inside the template after <?php the_content?>. I make a dummy page called Debug (or whatever, as long as the shortname/slug is debug, load the updated template, go in my browser to http://myspecial.site/debug and watch it do its work (usually I watch it spawn PHP errors, which I fix and try again). When its done, I remove the code or the entire page-debug.php template.

So…. for the 1/2 a lunatic who might still be reading this post (I write it mainly for my notes, after all), I started keeping these code fragments as GitHub gists, see the code I used to “seed” the values for existing posts.

Basically, it runs through all old posts, it does the query to fund how many responses there up, adds that bit of post meta data, and then, for fun, puts some random number in for a guessed number of visits. Do you see why I love data so much?

Now that I have this data, besides adding it to the display of each Daily Create, I can do more fun stuff. I built a shortcode that allows me to display anywhere in WordPress (page or widget) a list of the Daily Creates with the most responses OR a list of the ones with the most visits. Or heck, both in one page, the new Daily Create Popularity Metrics Display (it is not yet validated on a blockchain 😉

popular-dcs

And I can show that the highest number of responses to a Daily Create is 41, and than happened twice

The shortcode has options to set the number to show, to hide the count, to hide the date, and to show the visits rather than the responses, like on a widget in the footer, I have it set to show the five Daily Create’s with the most responses, and to hide the date, this code is

If you are hungry code the code, I got it right here. I find shortcodes insanely useful for getting at information that is sitting there in the database and using it anywhere in WordPress.

Data. Popular. Hand rolled code.

I wana be more popular than the rest
I’ll do anything anything to impress
Everyone is average but I just wanna be the best
Everything thing I do has to be a fact of interest
Popular


Top / Featured Image: Found the direct way, a Google Images search on “popularity” (filtered for licensed to reuse, natch!). I’m not 100% sure of the meaning of the figures at the bottom, so fill in your own meanin, it’ just a metaphor darnit.

This image is a flickr photo by psd https://flickr.com/photos/psd/8597760704 shared under a Creative Commons (BY) license

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for CogDog The Blog
An early 90s builder of web stuff 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. And he is 100% into the Fediverse (or tells himself so) Tooting as @cogdog@cosocial.ca

Comments

  1. Strikes me again how putting shortcodes together can be classed as a (useful) recreational computing activity: have itch, treat it as a puzzle, puzzle over shortcode to solve it, realise shortcode can be made more general, end up with useful, reusable thing, (learn something along the way). 🙂

Leave a Reply

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