Quick Code Fix: Disqus 'Same Comment' Problem in Wordpress

I found a quick fix for a comment repeating problem I was seeing on my blog with disqus. I'm not a PHP or wordpress hacker (normally) so take this with a grain of salt.

I had recently updated my blog's theme to a new, major-version-release of Thesis. After the update all of my posts began showing the comments from the same post. I tried uninstalling and reinstalling the disqus plugin but that didn't work.

A little googling and I found a few posts on the subject:

So now I had a hint as to the problem but I didn't find a clear fix on the googs.

Looking at the generated HTML for each post I found that the disqus_identifierwas being set to a single space ' '.

Spelunking around in the plugin code, first I found where the javascript was being created in comments.php.

<script type="text/javascript">
/* <![CDATA[ */
    var disqus_url = '<?php echo get_permalink(); ?>';
    var disqus_identifier = '<?php echo dsq_identifier_for_post($post); ?>';
    var disqus_container_id = 'disqus_thread';
    var disqus_domain = '<?php echo DISQUS_DOMAIN; ?>';

Following that function I found defined in disqus.php:

function dsq_identifier_for_post($post) {
    return $post->ID . ' ' . $post->guid;
}

So it looked like the $post global wasn't available or being set.

This made me believe that the structure of the new version 2.0 Thesis templates has somehow resulted in the disqus plugin code not being able to access the $post global variable. I changed the function to use the get_the_ID() and get_the_guid() functions and now all seems to be working again.

function dsq_identifier_for_post($post) {
    return get_the_ID() . ' ' . get_the_guid();
}

As far as I can tell from the wordpress docs, those functions should, in theory, work in the same scope as the $post global so I'm not sure why the functions work but the global doesn't (and I stopped poking around wordpress at this point since it started working, nothing like being a monkey in the control room of a nuclear reactor :) ).

Thought this might help someone out there that hits this specific problem.