creative commons licensed ( BY-NC-ND ) flickr photo shared by cszar

If you thought the last post on Under the Hood of the Thought Vectors Site was a gory on code, maybe click next. But for my own sake, some more write-ups of more recent twiddlings.

Got RAND() Post?

A trick I have used on ds106 and some following syndication hub sites is to add a link that generates a randomly chosen syndicated post, like a call to comment on a randomly drawn post. It uses a direct MySQL query that uses the

syntax, but I was stumped because in all my tests on thoughtvectors, it was not only bad random, it was ignored. I always got the most recent post.

It was a hunch that the WP Engine hosting might have this bit of MySQL disabled; it is said to be a resource hog. Tom confirmed this; offering to turn it on, but I wanted to dig in a bit for an end around. As usual I found an answer in Stack Overflow to make it part of the WHERE condition.

The previous post had a long section on the shortcode function I wrote to add the list of syndicated blogs (or those within just a category) to use in a sidebar widget. It already knows the category ID if we are on a category archive view.

So we can see on the sidebar of all posts associated with UNIV 200 Section 5

random post link

At the bottom is a link for a random post (oops, the 2 week thing has been removed, but that does not matter for this example)

The group= parameter represents the WordPress ID number for that category.

To be able to pass custom parameters like this to a wordpress template, you have to add some code to your functions.php file to define the query parameter, like:

In this case I can then use URLs like to send a variable named “catty” to the template for my gizmo page.

Did i lose ya? This is the easy part.

To make the random page thing work, I make a theme template named page-random.php. If I then create a WordPress Page and select it from the Templates menu on the right (or give it a slug of “random”), but template page takes over from the normal display of page content (in my case the content in the body of the page of the WordPress editor is ignored)

The PHP for this template below is pretty basic (hah!). It looks to see if we got a parameter with a category ID; if not we give it a default value. This goes into a custom MySQL query that has the magic condition to pull a random post, between 1 and the maximum ID used in the table.

We get one result, where the URL for the post (in Feed WordPress this is the external URL) we fetch from guid, and then use a command to redirect the browser to the random site:

Got it?

Some OPML Please

An even more arcane acronym than RSS is Outline Processor Markup Language (OPML) — but they got together. OPML is an XML (that never helps in explaining, does it?) structure for organizing outline lists of data. For our purposes, it is a nifty way to create a subscription list for all feeds in a group, e.g. all feeds for the students in Jon Becker’s section, so you can use it to import into Feed Readers, and in our case, other sites running Feed WordPress.

It allows you to mass import feeds.

Jon actually asked me about it, and it is handy since a number, if not all of the section “Clubhouse sites” (ones specifically managed by course instructors) like Jon’s Team Oneder one may want to run their own syndication.

We have syndication all over da place.

Generating OPML from the Feed WordPress data was something I had also done for ds106 (see sidebar where you can get an OPML for all open participants).

Again, since we are organizing feeds by categories that are applied at the point of syndication, its pretty easy to roll into the archives template; again for UNIV 200 Section 5, at the top right we have an RSS feed (WordPress makes that easy) and an OPML download link.

section 5

Just to be inconsistent, I have this php script, tv-opml.php at the root level of the site; it makes no difference. Here it is in parts…

We start by defining the default category ID (in case none are passed), and the base for the title string of the OPML output. Asking for wp-load.php is a way we can access the WordPress functionality but where we are not doing any output related to the theme (in this case the output is downloaded).

Next we look for a parameter group=XX in the URL to define the category ID used in the database query. The get_category lets us get at info related to the category, such as its full name $mycat->name.

No we run the query- we are searching the links table, where Feed WordPress stores its info, specifically for a string like “{category#45}” to if 45 is the category id we are looking for.

We should up with results of all links that match the category ID.

Lastly, we start returning the output in OPML structure. The header() function lets us generate a download rather than something that appears in the browser, the rest is just the formatting that makes it an OPML file.

But Wait, There’s More!

The code fun never stops, but I will break this one here. The latest bits happened last night, and what we have in a trial version is a site that is syndicating all of the comments from the UNIV 200 blogs; generating an OPML for comment feeds was just an extension of the function above.

Stay tuned for that stuff.

Let’s see how the patient is doing.

creative commons licensed ( BY-NC-ND ) flickr photo shared by cszar


The post "Thought Vectors Code Guts, Part Deux" was originally zapped with 10,000 volts and declared "It's ALIVE" by Dr. Frankenstein at CogDogBlog ( on June 16, 2014.


Leave a Comment

All fields are required. Your email address will not be published.

Creative Commons License
Thought Vectors Code Guts, Part Deux by CogDogBlog is licensed under a Creative Commons Attribution-Share Alike 4.0 License.