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.

Fix: WPtouch Pro Plugin Breaks WordPress 3.1’s Admin Bar

WordPress 3.1 rolled out today and brought with it several new features, one being the WordPress admin bar. This feature has been previously available to WordPress.com users and is now available to self-hosted sites (like Techerator), and gives logged-in users access to page controls without having to leave the site.

Sites that use the popular WordPress plugin WPtouch or WPtouch Pro will notice that this plugin breaks the new admin bar. While WPtouch is active, the admin bar will not appear on the main site or in the WordPress Dashboard (it took quite a bit of troubleshooting to narrow down which plugin was the culprit!).

The fix is very simple, and the team behind WPtouch has confirmed they are aware of the problem and intend to fix it.

The Fix

This guide requires that you edit plugin files in your WordPress installation. If you do not feel comfortable following this guide, please seek assitance before proceeding. And above all – make a backup!

Step 1: Navigate to the WPtouch plugin themes folder on your web server. Mine was located in /wp-content/plugins/wptouch-pro/themes/classic (If you’re using a different mobile theme, select that instead of classic.)

Step 2: Edit root-functions.php

Step 3: Locate the following code:


if ( function_exists( 'show_admin_bar' ) ) {
add_filter( 'show_admin_bar', '__return_false' );
}

And comment it all out:


//if ( function_exists( 'show_admin_bar' ) ) {
//    add_filter( 'show_admin_bar', '__return_false' );
//}

That’s it! Your WordPress 3.1 admin bar should now be visible on your site. To enable or disable the admin bar, visit your User settings under Users –> Your Profile in WordPress.