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:

[code lang=”xml”]

<span class="dsq-postid" rel="{unique post identifier}">{number of comments}</span>

[/code]

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:

[code lang=”php” firstline=”692″]

function dsq_comments_number($count) {
global $post;

if ( dsq_can_replace() ) {
return ‘<span class="dsq-postid" rel="’.htmlspecialchars(dsq_identifier_for_post($post)).’">’.$count.'</span>’;
} else {
return $count;
}
}

[/code]

Replace it with:

[code lang=”php” firstline=”692″]

function dsq_comments_number($count) {
global $post;

return $count;
}

[/code]

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:

[code lang=”php” firstline=”697″]

function dsq_comments_text($comment_text) {
global $post;

if ( dsq_can_replace() ) {
return ‘<span class="dsq-postid" rel="’.htmlspecialchars(dsq_identifier_for_post($post)).’">View Comments</span>’;
} else {
return $comment_text;
}
}

[/code]

Replace it with:

[code lang=”php” firstline=”697″]

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

[/code]

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

[code lang=”php”]

<?php comments_number(”,’ / 1 Comment’,’ / % Comments’); ?>

[/code]

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.


Posted

in

,

by

Comments

68 responses to “Fix: WordPress 3.1 and Disqus Plugin Error When Returning Comments Count”

  1. Easylancer Avatar
    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

    1. erald mariano Avatar

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

  2. Bay Phillips Avatar

    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.

  3. ground penetrating radar Avatar

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

  4. Rishi Talreja Avatar

    Incredible. Works! Thank you 🙂

  5. DSPICKETT Avatar

    Awesome thanks for your help! 🙂 Problem solved.

  6. PelFusion Avatar

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

  7. PelFusion Avatar

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

  8.  Avatar
    Anonymous

    This is awesome, thank you! Much love, ♥

  9. Sci Shark Avatar

    Thanks so much for this!

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

  10. Lars Avatar

    great Thanks for this fix

  11. Kevin Avatar
    Kevin

    Awesome! Thanks for the work around. Works great!

  12. Kevin Avatar
    Kevin

    Awesome! Thanks for the work around. Works great!

  13. Tyler Hayes Avatar

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

  14.  Avatar
    Anonymous

    Thanks a lot!

  15. Cal Avatar
    Cal

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

  16. Cal Avatar
    Cal

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

  17.  Avatar
    Anonymous

    Omg thank y!

  18. Evan Grist Avatar

    Worked great, thanks for this.

  19. Pubudu@TechHamlet Avatar

    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 😀

  20. Melissa Martin Avatar

    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?

    1. Evan Wondrasek Avatar

      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.

      1. Evan Wondrasek Avatar

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

      2. Nick Bramhall Avatar

        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.

        1. Evan Wondrasek Avatar

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

          1. Andreas Engström Avatar

            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.

          2. Evan Wondrasek Avatar

            Hi Andreas:

            Try using this code to display your comments:

          3. Andreas Engström Avatar

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

  21. […] issues with WordPress when it comes to displaying comment counts on the main blog page. Thanks to this helpful fellow I’ve fixed the main issue, but the comment link is now quite small and easy to miss — […]

  22. 1uk3 Avatar

    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.

  23. sam Avatar
    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! 🙂

  24. […] I did exactly that, finding no solution that actually worked. I found one that was close (here), but didn’t get me exactly where I wanted to go. However, it got me much of the way, and my PHP […]

  25. buzzparas Avatar

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

  26. michal rock Avatar
    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

  27.  Avatar
    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.

  28. Web Design Outsource Avatar

    Thanks for sharing this one. This is very helpful.

  29. Mental Arithmetic Avatar

    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. 

  30. WordPress Designer Avatar

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

  31.  Avatar
    Anonymous

    NOTHING 

  32. Web Designer Avatar

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

  33. Sub-word line driver circuit Avatar

    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.

  34. wordpress themes Avatar

    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.

     

  35. wordpress themes Avatar

    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.

     

  36. Android app development Avatar

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

  37. Hindi Sms Avatar

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

  38. mobile apps Avatar

    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.

  39. Website design Avatar

    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!

  40.  Avatar
    Anonymous

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

  41. David Avatar

    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.  

  42. David Avatar

    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.  

  43. David Avatar

    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.  

  44. David Avatar

    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.  

  45. blogdesigningcompanyinusa Avatar

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

  46. Small business IT support Crys Avatar

    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.

  47. Small business IT support Crys Avatar

    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.

  48. Small business IT support Crys Avatar

    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.

  49. Vipin Ramdas Avatar

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

  50. thanghn92 Avatar

    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

  51. Ashley Jones Avatar

    Blog commenting is absolutely one of the easiest way to get backlinks to a new or old website as long as you commented with great and relevant feedback about the article or posts. Blog commenting is an inbound links that helps to gain more links pointing to your site. It is also one of the SEO methods to be used.

  52. Html Pro Avatar

    HI Evan Wondrasek.

    Thanks for sharing the detail solution of disqus comment count, It really helps, but their is alot of different and quick way to resolve it in a min but your article is more in detail and any layman can easily understand and implement it.
    Thanks and keep resolving the issue.

  53. Juli Avatar

    Awesome fix is fast planning, I just overhauled today and confronted this correct issue althought I couldn’t discover where the issue was originating from until the point that you brought up out. Here is a fix which is all the more in reverse perfect incase the client chooses to move back to more seasoned variant of wordpress without making sure to reestablish the progressions.Call Girls in Goa

  54. dubai sugar Babes Avatar
    dubai sugar Babes

    I really like and appreciate your blog article.Much thanks again. Really Great.
    http://www.dubaisugarbabes.com

Leave a Reply