Remote Comments Plugin (a FWP “AddOn”)

A long time ago, I blogged about some code I had written for ds106 that made it possible to show how many comments had been left on a post that was being syndicated (via FeedWordPress) from elsewhere. The code was pretty simple — it was based on the fact that some feeds (including ones originating from WordPress) pass a parameter called “wfw:commentRSS” which contains the RSS feed of the comments on an individual post. FWP stores this as a custom field for each syndicated post. So, it’s pretty easy to grab that RSS feed URL, fetch the feed, and then count the number of items in it.

When I came up with this technique back in 2011 I implemented it by editing the theme for ds106. Eventually, however, we removed the code. I seem to remember we thought it was impacting performance on the site. Depending on how many posts were displayed on the home page, that was a lot of RSS retrieval that needed to be done before the page could be displayed.

This week, Jim asked me if I could put the code on the site being used for the (awesome, new) Digital Scholars Initiative at UMW. I went ahead and did it, and in doing so I thought perhaps it was time to return to this code and see if I could improve it.

So, I spent today developing a plugin that will allow this code to be used fairly easily on other sites. I’m actually embarrassed how easy it was to write this “plugin.” It’s literally a single function.

There’s a lesson here about the iterative, developing nature of my relationship with coding. As I’ve blogged about in the past, my progress in this area has been years in the making, and oftentimes I’ve set out to learn something, been thwarted by my own lack of understanding, and abandoned the project — only to discover (weeks/months/years) later that I’ve developed the knowledge I now need to solve the problem. That was the case in this instance. In January 2011, figuring out the basic code to do this in the theme seemed huge. Trying to generalize it seemed insurmountable. Since then, my work on other projects has taught me just enough that when I returned to this project this week, I discovered I kind of already knew how to do it (although I didn’t know I knew it until I thought about it a bit — spent some time on Google).

In any case, like I said, the plugin is so dead simple, it’s embarrassing. It is made possible by a filter in WordPress, “get_comments_number” which allows me to intervene in the process of counting up the comments on posts. Basically, my function checks to see if the post is syndicated, and, if it is, counts the items in the comment feed — just like I had been doing in the theme years ago.

I added one additional feature to the plugin. I’m storing the comment count and a time stamp of when the count was made in two additional custom fields on each post. Then, every time the function runs, it checks to see when the comments were last counted. Right now, I have it set to only refresh if it’s been more than 10 minutes since the last count. Eventually, I’m going to add a plugin option so that this cache time can be set in the backend of WP.

There is a caveat to using this plugin — it depends on a site’s theme using WordPress’ built in comment counting functions. If the theme developer has gotten all fancy and written her own code for dealing with comment counts, the filter function I’ve added will never run.

The other thing that I need to point out is that this plugin (just like the theme hack 3 years ago) will only work if FWP is able to grab that wfw:commentRSS parameter out of the site’s RSS feed. On the Digital Scholars Initiative site, for example, one user is using Drupal which doesn’t expose that parameter in the feed. Without it, there’s no way for this plugin to grab a count of comments. So, there is some inherent limitations to this. As in 2011, this is the best I can do for now, and it’s a bit better than what I was able to do before.

One step at a time.

If you’re interested in trying it out on sites where you are syndicating content with FWP — and telling me about any issues/errors you see, feel free to download a zip file of the Remote Comments plugin here.

I do plan on putting this up in the WP plugin repository (even if it is embarrassingly simple), but that requires me to wrestle with WP’s SVN, and I don’t have it in me to fight that fight right now.