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.
5 thoughts on “Remote Comments Plugin (a FWP “AddOn”)”
I think wordpress is the only platform that provide a comments feed that you can get this info.
The overhead depends on how many things being iterated in a loop; so on a typical blog feed 10 may not be bead, but on the assignments site where it checked the examples (and the page already had a number of database calls for other things), we list all. So what happens is, for every page view, it goes out and has to fetch the feed and count. That can be a lot of overhead and, IMHO, un-necessary repeated traffic. To do it more efficiently, it would be better (and harder) to tap into the FWP process where it checks the feed, get that count, and store the comment count as post metadata in the archived post; then for each loop, you are just fetching a local value rather than going out and grabbing via a feed (and for something that may not change that often).
I do something like that on the assignments site (ds106 and the new one), that every time someone views the assignment, it increments a view counter stored as post meta, so elsewhere (like a listing of assignments) it can list numbers of views, examples, etc.
Yep — this is why I added the fields to save the comment count/timestamp and essentially “cache” the count based on a set number of minutes. I set it to 10 minutes (hard coded now, but eventually I’ll make that an option that can be set), but I’ll probably play around with that number.
Doing this process as part of the FWP call is tricky — grabbing it when a post is initially syndicated is pointless, of course, because you’re catching the comment count right after the post is published so it’s not really an accurate reflection of activity. FWP won’t update posts unless something changes in the feed, and since this is all built around an RSS parameter that doesn’t change, I’m not sure how to trigger it for future comments on already published posts.
I did think about possibly doing this is a cron running in the background every hour or so, but, honestly, I don’t want to run this every time for every post in a site. That would be a ton of unnecessary overhead. I guess it could be written to only run on the latest “X” number of posts, but then if people browse more deeply into the archives, the counts aren’t accurate.
I figure with the way I have this coded now, on a typical site (displaying the last 10 posts), you’d be checking 10 feeds every ten minutes for every page of posts. I don’t *think* that’s too onerous — and I can always increase the time increment to 60 minutes or something.
That’s totally sensible! Will look to add back to ds106
That’s the part I really like- the evolution over long periods of time and fact that things do quite often cycle back around.