0

What I Am Trying To DO

I am using a plugin called Comments Evolved which aggregates comments that are tabbed from Faceook, G+, and WordPress native.

I am trying to replace the native comments_number number with the number from the plugin's aggregate count.

The Problem

I want to do this via functions.php but I am having trouble as it seems to only be pulling the Facebook comments count. I suspect my filter has not worked and hence it's only pulling what WordPress native would pull.

What I've Tried

Currently I am using this filter:

// Replace native comment count with Comments Evolved comment in native comments_number function
function comments_evolved_number() {
    $number = comments_evolved_get_total_count();
}
apply_filters('comments_number', 'comments_evolved_number');

but it doesn't seem to be doing the trick as it only shows the number of comments in the Facebook tab.

In my index.php, I am using this to pull the comments:

        <?php comments_number( 'Say somethin\'!', '1 comment', '% comments' ) ?>

I've also tried add_filter but that seems to do nothing as the comments aren't output at all. I've searched everywhere, forums, WordPress codex, plugin GitHub, and even looked through similar threads dealing with Disqus comments, but I can't find the reason my filter is failing.

What am I doing wrong?

UPDATE 1

Tried user birgire's answer and it's much cleaner, logical and better than any answer I can come up with, but for some reason it only works partially.

On testing, it seems that it does Not pull the Facebook comment count, though it pulls and aggregates all the rest.

Comments Evolved constructs comments_evolved_get_total_count() like this: function comments_evolved_get_total_count() { $total_count = 0;

  $wordpress_count = comments_evolved_get_wordpress_count();
  //$wordpress_count = get_comments_number();

  $gplus_count = comments_evolved_get_gplus_count();
  $trackback_count = comments_evolved_get_trackback_count();
  $facebook_count = comments_evolved_get_facebook_count();
  $disqus_count = comments_evolved_get_disqus_count();

  $total_count = $total_count + $wordpress_count + $gplus_count + $trackback_count + $facebook_count + $disqus_count;
  return $total_count;
}
//add_filter('get_comments_number', 'comments_evolved_get_total_count', 4269);

The Facebook comments_evolved_get_facebook_count() is constructed like this:

function comments_evolved_get_facebook_count($url = "") {
  if(empty($url)){ $url = get_permalink(); }
  $link = 'https://graph.facebook.com/?ids=' . urlencode($url);
  $link_body = wp_remote_retrieve_body(wp_remote_get($link));
  $json = json_decode($link_body);
  return $json->$url->comments;
}

I don't see any errors in that and in other places it pulls the correct Facebook count (I think -- not sure).

What did work but doesn't seem efficient/satisfactory

function comment_count_agg() {
  $total_count = 0;

  //$wordpress_count = comments_evolved_get_wordpress_count();
  $wordpress_count = get_comments_number();

  $gplus_count = comments_evolved_get_gplus_count();
  $trackback_count = comments_evolved_get_trackback_count();
  $facebook_count = comments_evolved_get_facebook_count();
  $disqus_count = comments_evolved_get_disqus_count();

  $total_count = $total_count + $wordpress_count + $gplus_count + $trackback_count + $facebook_count + $disqus_count;
  return $total_count;
}
add_filter('comments_evolved_get_total_count', 'comment_count_agg', 4270);
add_filter('get_comments_number', 'comments_evolved_get_total_count', 4271);

...though I am not exactly sure why.

I tried it this way because (a) I gifured something in the plugin is messing with the aggregate count before birgire's filter is applied and (b) because I thought maybe the priorities were somehow an issue.

Can you tell me why my messy function works but birgire's doesn't, and if there is some way I can make it more efficient?

UPDATE 2

User birgire's answer is indeed full and correct, but the Facebook comment counts were simply not being pulled in because the domain I was working on was over the Open Graph API call limit.

The error I got via the returned JSOn object from Facebook was:

stdClass Object ( [error] => stdClass Object ( [message] => (#4) Application request limit 
reached [type] => OAuthException [is_transient] => 1 [code] => 4 [fbtrace_id] => AgE3COAZ+tj ) )

I've provided a detailed answer here regarding FB OG API limits.

0

Few issues here.

  • use add_filter() instead of apply_filters(), to register your custom callback.

  • return the value within your filter's callback.

  • use the get_comments_number filter instead of the comments_number filter, since you only want to change the number and not the whole output of the comments_number() function.

  • prefix the name of your custom callbacks, to avoid possible name collisions.

So try this version instead:

function wpse_comments_evolved_number( $count ) 
{
    // Override the comment count
    if( function_exists( 'comments_evolved_get_total_count' ) )
        $count = comments_evolved_get_total_count();

    // We must then return the value:
    return $count;
}
add_filter( 'get_comments_number', 'wpse_comments_evolved_number');

where we make sure the function comments_evolved_get_total_count() exists. We don't want to bring down the whole site, if that plugin is uninstalled.

Hope it works.

  • Thank you for the prompt and insightful response. It works but not completely. It doesn't pull in Facebook count for some reason. Facebook is enabled inside plugin and I see tab, and I've tried adding new comments but they don't get added to the count unless I use my messy function. Any idea why? I've updated my question. – Andre Bulatov Feb 18 '16 at 0:40
  • So annoying that I still can't upvote your answer :/ – Andre Bulatov Feb 18 '16 at 0:40
  • Hard to say why only facebook count isn't working. Note the possible infinite loop if you call get_comments_number() within the get_comments_count filter. Try the $count callback input instead. Looks like you're not using any cache, so this could slow down your site. @AndreBulatov – birgire Feb 18 '16 at 11:14
  • Thanks man! I've actually figured out the Facebook issue -- apparently the domain is over the Open Graph API limit. – Andre Bulatov Feb 18 '16 at 17:57

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.