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.