3

I am a bit of an amateur when it comes to PHP and WordPress but I have rigged up the following code:

function fb_comment_count($url = 'some url here') {
$filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url);
$json = json_decode($filecontent);
$count = $json->$url->comments;
if ($count == 0 || !isset($count)) {
    $count = 0;
}
echo $count;
}

What it does is it retrieves the comment count from the Facebook Graph and displays it on a page. For it to work, I have to manually declare the url for each call.

What I'm having a hard time with, is setting it up so that when you call the function in the template, it defaults to the post's permalink. I honestly have tried everything that has come to my mind.

get_permalink() doesn't work when you declare the arguments in the function.

Any help would be GREATLY appreciated. Thanks!

  • Finally got it to work the way i wanted it to. Turns out you shouldn't use file_get_contents() in WordPress. I used wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=>false))) – Zack Austin May 30 '11 at 17:54
5

Final version of code used:

    function fb_comment_count($link = 'link') {
global $post;
$url = 'https://graph.facebook.com/';
$posturl = get_permalink($post->ID);
$url .= $posturl;

$filecontent = wp_remote_retrieve_body(wp_remote_get($url, array('sslverify'=>false)));
$json = json_decode($filecontent);
$count = $json->comments;
if ($count == 0 || !isset($count)) {
    $count = 0;
}

$comments = $count;
if ($count == 1) {
    $comments .= ' Comment';
}
elseif ($count == 0) {
    $comments = 'Leave a Comment';
}
elseif ($count > 1) {
    $comments .= ' Comments';
}
if ($link == 'nolink') {
    echo $comments;
}
else {
    echo '<a href="'.$posturl.'#comments" title="Comments for '.$post->post_title.'">'.$comments.'</a>';
}
}
  • +1 for using WordPress' HTTP API. – VicePrez May 31 '11 at 1:42
  • @Zack, thank you for this code. I am trying to implement it into my own site but I can't get the correct number of comments to show for the attachment pages. I have created a thread here: wordpress.stackexchange.com/questions/23409/… If you have any ideas, I'd love to hear. Thanks again. – jwp Jul 19 '11 at 17:49
1

Try this and see what it gives you:

function 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 = 0;
}
echo $count;
}
  • Thanks! That works wonderful to get the url! Now I just can't get the thing to work with the data from Facebook... – Zack Austin May 30 '11 at 16:54
  • @Zack did you end up getting it to work? – VicePrez May 31 '11 at 1:41
  • 1
    This is making the archives and index load so slowly. Any way to speed this up? – Picard102 May 28 '12 at 2:38
1

Seems extremely complicated when you could just use this:

<fb:comments-count href="<?php echo get_permalink($post->ID); ?>"></fb:comments-count> Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.