Fix: WordPress 3.1 and Disqus Plugin Error When Returning Comments Count

If you use the popular 3rd-party commenting system Disqus in your WordPress-powered website, you may have noticed that some errors appeared in your Posts lists after upgrading to WordPress 3.1.  The specific error you see would be something like:

Warning: number_format() expects parameter 1 to be double, string given in /wp-includes/functions.php on line 155

I’ve had problems with Disqus messing with WordPress comment counts in the past, and since those minor problems were never properly resolved, they caused more severe problems when WordPress updated their Posts view in version 3.1.

Essentially, Disqus takes the WordPress comment count and reformats it internally to wrap it in an identifying span that follows the format:


{number of comments}

Disqus effectively usurps WordPress’s comment count (which isn’t a big deal by itself), but the added HTML around the comment count is what breaks WordPress 3.1.

Why It Breaks

In the WordPress core file /wp-admin/includes/class-wp-list-table.php, WordPress makes a call to the get_comments_number() function and passes it to its internal function number_format_i18n().  Since Disqus has replaced the normal value returned from get_comments_number() with its own value wrapped in HTML, this breaks WordPress’s number_format_i18n function which expects the value to be a double instead of a string.

After quite a bit of troubleshooting, I figured out a way to fix this by making a small change to the Disqus plugin.

How to Fix It

Note: This method involves editing PHP files for WordPress plugins on your web server. If you do not feel comfortable following this guide, please seek assistance. And above all – make a backup!

Step 1: Edit the file /wp-content/plugins/disqus-comment-system/disqus.php

Step 2: Locate the following code at line 692:


function dsq_comments_number($count) {
 global $post;

 if ( dsq_can_replace() ) {
 return ''.$count.'';
 } else {
 return $count;
 }
}

Replace it with:


function dsq_comments_number($count) {
global $post;

return $count;
}

Step 4 (optional – this will fix comment counts in the front-end of your blog if using the comments_number() function in your theme): Locate the following code at line 697:


function dsq_comments_text($comment_text) {
global $post;

if ( dsq_can_replace() ) {
return 'View Comments';
} else {
return $comment_text;
}
}

Replace it with:


function dsq_comments_text($comment_text) {
global $post;
$number_of_comments = get_comments_number();
return $number_of_comments;
}

Finally, to display the comment count in your WordPress theme, use the following code wherever you want to display “X Comments”:




This should fix the comments count in your WordPress Posts view, and make comments appear correctly on your blog if you use the comments_number() function in your theme.

  • Easylancer

    Great fix is quick timing, I just upgraded today and faced this exact problem althought I couldn’t find where the problem was coming from until you pointed it out. Here is a fix which is more backward compatible incase the user decides to roll back to older version of wordpress without remembering to restore the changes.

    https://gist.github.com/842839

    • i couldn’t agree more. i was totally lost and don’t know where to start.
      thanks for digging, Evans!

  • Thanks for this! I had gone in and edited the WP core file; however, it’s definitely not the correct way to go about these things. I hope Disqus updates their plugin ASAP to fix the issue.

  • I hope Disqus updates their plugin ASAP to fix the issue.

  • Incredible. Works! Thank you 🙂

  • Awesome thanks for your help! 🙂 Problem solved.

  • what i did to solve this, activated back the older version of disqus

  • what i did to solve this, activated back the older version of disqus

  • Anonymous

    This is awesome, thank you! Much love, ♥

  • Thanks so much for this!

    I had the problem on my website after installed jetpack (disqus was already installed and working fine)

  • great Thanks for this fix

  • Kevin

    Awesome! Thanks for the work around. Works great!

  • Kevin

    Awesome! Thanks for the work around. Works great!

  • This is great stuff. Thanks for doing this, we’ll be sure to incorporate the fix in our next plugin release.

  • Anonymous

    Thanks a lot!

  • Cal

    I wish a came upon your post earlier, thank you very much!

  • Cal

    I wish a came upon your post earlier, thank you very much!

  • Thank you!

  • Anonymous

    Omg thank y!

  • Thanks for the hack!

  • Worked great, thanks for this.

  • Thanks a lot my friend for the post! I saw this error in my php error log. Thats how i noticed it. 😀 Now its fixed.. Thanks again 😀

  • Oh my gosh, thank you so much.

    Quick question: now I have comment counts (yay!), but the comment link text is gone: where it used to say “X comments” in nice, medium-sized theme-coloured letters, it now shows an extremely tiny number of comments next to my theme’s small icon. How can I make this better?

    • Hey Melissa,

      Glad it helped! Using the following code in your WordPress theme should restore the “X comments” functionality:

      If you need any help using this or styling the text to match your theme, send me an email at evan at techerator dot com and I should be able to give you a hand.

      • Wow, Disqus really had its way with the code I posted in the comments. I’ll try to email you.

      • Hi Evan. Thanks for your fix in the main blog post. I too now have the problem with the “X comments” functionality as Melissa experienced. Would you mind also e-mailing me the correctly formatted code for this fix please? I can’t seem to quite get it to work properly based on what you posted? Many thanks.

        • Hey Nick, I sent you an email. I’ll update this article with that information as well for future reference.

          • Hi, I used your fix and it worked great but as some others noted the X comments function went away somehere, probably to Hawai. If you have time to respond to this issue it would be great. But I understand if you are bussy with other things. At least I have the number of comments now. 😉 Thanks alot.

          • Hi Andreas:

            Try using this code to display your comments:

          • I have not tried it yet, but I do suspect it will work. 

  • Pingback: Blogkeeping Note: Comments » nothing in winnipeg()

  • I’ve only just noticed that I had this issue across my WordPress blogs.

    Thanks for the fix – very quick and easy but would have taken me an age to figure out on my own.

  • Really tanks!!

  • sam

    I think you can also simply comment out Disqus’s ‘get_comments_number’ filter, e.g.,

    // add_filter(‘get_comments_number’, ‘dsq_comments_number’);around line 1125 of disqus-comment-system/disqus.php, or useremove_filter(‘get_comments_number’, ‘dsq_comments_number’);

    elsewhere, maybe?

    Thanks for saving me time debugging this! 🙂

  • Pingback: Fix: Disqus for WordPress showing “View Comments” instead of comment count [Updated] | L’histoire de sa vie()

  • after hurdling with the problem for hours,i re-installed the plugin and tried again and it worked perfectly. anyway thanx for nice share.

  • michal rock

    I think for understanding of this every blogger should need to learn some basics of HTML so he can easily edit the problem in blogs without anyone’s help. Disqus is really become more and more popular comment system in today’s time.

    psd to html

  • Anonymous

    It is one of the best way to fix it but if you will move it into the psd code it will more helpful to you for making a high quality design and you can make it from c sharp also but the way of psd is easy.

  • n3u

    Thanks!

  • Thanks for sharing this one. This is very helpful.

  • You should expect downtime and errors especially when Disqus would release a new version. It’s just sad that some people say Disqus is selling users’ data to third party. I do hope they don’t do that. 

  • Nice share. It is really very helpful and only few people can help upto this level. Thanks champ….

  • Anonymous

    NOTHING 

  • Nice, thanks for your tips to resolved this problem….

  • Thanks for this! I had gone in and edited the WP core file; however, it’s definitely not the correct way to go about these things. I hope Disqus updates their plugin ASAP to fix the issue.

  • WordPress.com is a WordPress backup and management, so you do not have to worry about backing up your article. Hundreds of servers hosted in your blog, so you never have to worry about it down.

     

  • WordPress.com is a WordPress backup and management, so you do not have to worry about backing up your article. Hundreds of servers hosted in your blog, so you never have to worry about it down.

     

  • many plugins for wordpress which i always use for my each blog.and Disqus plugin is one of them.

  • I adore your wordpress design, where did you obtain it through?

  • The spectrum of this industry is wide and so taking into consideration the demand and necessity of your users is very important. When you are developing your own app for the first time, make sure to hire a programmer that will help you reach your customers regardless of their location.

  • To stand out in the crowd and achieve your online business goals make sure you give your web design serious consideration. Invite your designer to be creative in their implementation and design!

  • Anonymous

     Thanks for post. I dun know from where to start. thanks for helping

  • Anonymous
  • my problem is that after uninstalling disqus my normal blog commenting system no longer works.   Any ideas on that?   (all the previous comments are in tact)   No one can make any comments now.  

  • my problem is that after uninstalling disqus my normal blog commenting system no longer works.   Any ideas on that?   (all the previous comments are in tact)   No one can make any comments now.  

  • my problem is that after uninstalling disqus my normal blog commenting system no longer works.   Any ideas on that?   (all the previous comments are in tact)   No one can make any comments now.  

  • my problem is that after uninstalling disqus my normal blog commenting system no longer works.   Any ideas on that?   (all the previous comments are in tact)   No one can make any comments now.  

  • yeahhh… disqus is one of the best commenting tool and working great for me too.

  • I just upgraded today and faced this exact problem althought I couldn’t
    find where the problem was coming from until you pointed it out. Here
    is a fix which is more backward compatible incase the user decides to
    roll back to older version of wordpress without remembering to restore
    the changes. especially when Disqus would release a new version. It’s just sad that
    some people say Disqus is selling users’ data to third party. I do hope
    they don’t do that.

  • I just upgraded today and faced this exact problem althought I couldn’t
    find where the problem was coming from until you pointed it out. Here
    is a fix which is more backward compatible incase the user decides to
    roll back to older version of wordpress without remembering to restore
    the changes.

  • I just upgraded today and faced this exact problem althought I couldn’t
    find where the problem was coming from until you pointed it out. Here
    is a fix which is more backward compatible incase the user decides to
    roll back to older version of wordpress without remembering to restore
    the changes.

  • Dude. You are too good. Thanks you fixed a painful issue for me. Cheers!

  • Thanks for this! I had gone in and edited the WP core file; however,
    it’s definitely not the correct way to go about these things gia vang giay nam thoi trang FiFa Online 3 thoi trang nu tu vi xem boi mon ngon am thuc

  • great Thanks for this fix; tu vi 12 cung hoang dao