Now with Facebook Comments

My coding skills are practically non-existant, but every once in a while I like to get my hands dirty and try to hack something together.

The blog previously had a Facebook Connect implementation which was a little buggy and limiting, so I cracked away one evening to implement the new’ish Facebook Comments Box. A fairly straightforward task I should imagine, but it still took me several hours to fine tune it.

I first grabbed the XFBML code from Facebook’s developer site and inserted it into my theme. Turns out that it conflicted with the old Connect hack, so I had to remove that to get the Comments Box to show up.

I was keen to retain the historic WordPress comments, so I demoted them further down the page, labelled them as “Archived WordPress Comments” and removed the capacity to leave new comments.

WordPress has a function to show the number of comments for a post:


However, I now wanted to show the true total which would aggregate the number archived WordPress comments and also Facebook comments.

The Facebook Open Graph API (my first ever interaction with an API!) allows you to grab the comment count and store it as a variable:

$source_url = get_permalink($post->ID);
$data = file_get_contents(‘’. urlencode($source_url));
$json = $data;
$obj = json_decode($json);
$fbcommentcount = $obj->{‘comments’};

Then this value can simply be added to the WordPress count:

$commentotal = $fbcommentcount + get_comments_number();

To show the comment count below each post, I replaced the existing function with the following:

<a href=”<?php the_permalink(); ?>#comments” class=”commentslink” title=”Comment on <?php echo the_title(); ?>”><?php echo $commentotal . ‘ Comment’ . ($commentotal == 1 ? ” : ‘s’); ?></a>

And that’s it. I suspect the code isn’t at all elegant, but it does the job and I’m kinda proud.

Next up:

  • Use the API to fetch the comments and store them behind the JavaScript for SEO.
  • Figure out how to calculate the total comment count across all posts.
  • Cache the comment count rather than making a call on each page request. 

Some useful posts during the hackathon session:

The plugin is fantastic. Much less friction for users (which should hopefully encourage more commenting), less spam (since people are attaching their real identity), more distribution through Facebook, better moderation tools, and other things.

Why not say hello below?

 Tags: Random   Published: 28th November '11

No Archived Wordpress Comments