e-mail: MP@MikePayne.co
phone: +1 (801) 949-7137
twitter: @the_mikepayne

Getting Facebook Comment Count Within WordPress

I’ve been working with the Facebook Comment integration into WordPress lately. Mostly because it can help cut down on spam, but also because of the grammer checker and social media benefit. I couldn’t really find a good solution online for pulling the number of comments on any given post, so here is what I ended up with. I included this in my functions.php file, and just put this: <?php get_fb_comment_count(); ?> in my single.php file where I want the comment number to be displayed.

function get_fb_comment_count() {
     global $post;
     $url = get_permalink($post->ID);
     $filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
     $json = json_decode($filecontent);
     $count = $json->$url->comments;
     if ($count == 0 || !isset($count)) {
          $count = 'No Comments';
     } elseif ( $count == 1 ) {
          $count = '1 Comment';
     } else {
          $count .= ' Comments';
     }
     echo $count;
}

Preventing Spam: Blocking Comments from No-Referrers

deny-comments-from-no-referrer

Web browsers store a lot of information about how you navigate the internet, and one of those metrics is called a referrer. A referrer is the page that you were on before the page you are currently on. By checking this browser option we can eliminate quite a few of our spammy comments from bots that crawl the internet and post links to certain ‘medical solutions’.  Bellow is another WordPress PHP Snippet that you can post into your functions.php and will check whether a referrer exists or not.

Read more
Copyright ©MMXI Mike Payne