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:

<span class=”comments-link”>
<?php if (is_syndicated()) {  // this just tests to see if the post is a syndicated post ?>
<?php if (get_post_meta($post->ID, 'wfw:commentRSS')) { // this tests to see if the custom field we're looking for actually exists
$comments_url = get_post_meta($post->ID, 'wfw:commentRSS', true); // this passes the posts comments feed URL into a variable
$comments_link = get_post_meta($post->ID, 'rss:comments', true); // this passes the link to the comments section of the post into a variable
$comments_rss = fetch_feed($comments_url); //this parses the feed variable into its parts
if (!is_wp_error( $comments_rss ) ) { // this tests to see if the parsed feed is viable
$maxitems = $comments_rss->get_item_quantity(); //counts the number of items in the feed
if ($maxitems !== 0) { // tests to see if there are more than 0 items
echo '<a href="'; // the next couple of lines spit out the HTML to display the comment count.
echo $comments_link;
echo '">';
echo $maxitems;
echo ' comments. </a>';
} else { // if there are no comments the next few lines are displayed (with a link to leave a comment).
echo '<a href="';
echo $comments_link;
echo '"> Leave a comment</a>';
};
};
}
} else {  // if it's not a syndicated post, just do the regular bit for comments?>
<?php comments_popup_link( __( 'Leave a comment', 'twentyten' ), __( '1 Comment', 'twentyten' ), __( '% Comments', 'twentyten' ) ); ?>
<?php } ?>
</span>

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):

add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 600;') );

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

15 thoughts on “Displaying Comment Counts on Syndicated WordPress Posts (using FeedWordPress)”

    1. After reading the awe-inspiring post on @mburtis ‘ blog on syndicated comments for the #ds106 site, I bid goodnite & go read Millennium 3.

  1. I’m new to FeedWordPress, and have the opposite problem. I do want all the comments to appear on the main blog, and have set the syndication that way. But it persists in saying “comments are closed” for the syndicated items, despite comments being open in the WP default and the FWP site-wide default settings.

  2. I’m going to try this now. I’m daunted by the “heavily modified loop” phrase, which I don’t understand, but when did I let a little thing like “for best results, cut into abdomen when removing appendix” stop me? Even if I have to look up the word “abdomen”?

    For displaying the latest comments across all posts, do you mean what I’m doing here? http://blogs.lt.vt.edu/vtclis12 ? A student gave me a tip: I simply subscribe to all the comment feeds in Google Reader, all in the same folder; Google Reader now offers an RSS-mix function that gives me a combined RSS feed for all the comments. Voila. This beats my other workaround, which is to construct a fake blog that aggregates all the comment feeds via FWP, after which I use the “post” feed from that fake blog to generate the RSS mix. (Many sorrows along the way with that workaround, though it does work, if one doesn’t mind sorrows, i.e. every spam comment ending up with that spammer as a contributor to the fake site, as FWP enrolls all syndicated sources as “contributors.”)

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.