Displaying Comment Counts on Syndicated WordPress Posts (using FeedWordPress)

So here’s something neat that I figured out how to do on Jim’s DS106 class (soon to be available on my own course site). As you may know, both sites are syndicating in feeds from students’ blogs using the amazing FeedWordPress plugin. We use this plugin all over UMW Blogs to allow faculty to manage course “mother” blogs into which students’ blogs are fed.

When we feed blogs using FWP, we almost always have the post on the mother blog link to the original posts on each individual blog. That’s a decision that reflects our belief that we need to encourage students to develop a sense of agency about their sites — each blog is not merely a cog in the greater course engine. Rather, it is a node that represents the real presence of a person in the class. We want readers of the course blog to make their way to the students’ blog and leave comments in that space — where each individual student can monitor and reply on his/her commenters.

However, one of the drawbacks has always been that on the course blog there is no way to witness the conversation that is developing around the posts that are syndicated into the site. Since the post on the course blog is never actually viewed or commented upon, the site always suggests that there are “No Comments” for the post — when, in actuality, if you click the link and go to the individual student’s blog, there may indeed be rich commentary there.

When DS106 was starting up, Jim mused that it would be very cool to finally crack this nut — how can we have information about the comments on each post trickle up into the course blog?

I decided to try and tackle this challenge this week, and, at first, I thought it was going to be fairly complicated. I know that in WordPress, at least, every post has an individual comment feed associated with it. But I wasn’t sure if the URL structure for those feeds would be easy to program. I’ve got the permalink for the post coming in when the post is syndicated, but depending on the permalink structure on the site, the actual feed URL may vary. In addition, not everyone in the class is using WordPress — how to handle folks in other systems?

I started by simply trying to solve the WordPress post comment challenge and leave the other systems for later. But, sure enough, when I simply tried to append what I thought the rest of the feed URL would be to the permalink URL, the code broke.

At that point, I noticed something interesting. Whenever FeedWordPress syndicates a post, it pushes some data from the RSS feed into custom fields. It seems like with each new version of FWP, more and more data has been getting pushed into these fields. While I was trying to figure out what to do with the post comment feed URL problem, I noticed that many (if not most) of the posts in the system had a custom field associated with them that was labeled “wfw:commentRSS”. Sure enough, the value of that field was the URL of the post comment feed!

(A bit of research turned up that “wfw:commentRSS” comes from the Well Formed Web project, and WP seems to use it in the creation of feeds by default. I admit I’m clueless when it comes to RSS format and namespaces, so if anyone wants to educate me on all of this, please do so.)

The bottom line is that I was able to fairly easily hack some code using a built-in WP function called “fetch_feed.” All this function does is parse a feed and then allow you to spit it back out in parts. I don’t actually have to do any spitting out — I’m just counting the number of items in the feed and building the little notation at the bottom of the post that indicates how many comments there are on the post. But, in this case, the link on that notation goes to the comment space of the original post (this URL, as it turns out, is also passed in a custom field when you pull a WP feed in using FeedWordpress).

Here’s the code I hacked into the Twenty-Ten child theme we’re using for the DS106 blog (FYI, I’m using a heavily modified loop for this site). Basically, you want to replace everything that is typically in the <span class=”comments-link”></span> with the following:

Honestly, we’ve been talking about wanting this bit when we use FeedWordPress for ages in DTLT, and it’s almost embarrassing how easy this was in the end.

There are still some bugs to figure out — the comment count is getting stuck on some posts, and I think that’s a caching issue with the parse function that I need to solve. Also, I’m not sure yet what’s happening with all the non-WordPress sites. Some aren’t working (resulting in nothing being displayed), but I need to track this down.

UPDATE: The incorrect comment count does, indeed, seem to be the cache on fetch_feed. The fix (which I found here) is to add the following to your functions.php file (The “600” represents the number of seconds to cache the feed for. So, this means I’ve set it for 10 minutes. I’m not sure if that’s going to be too much of a load on the server. We’ll see):

The next nut? How can we show the latest comments across all of the posts. I think this is much tougher proposition.