Tame DISQUS Comments in WordPress

Share This Post

DISQUS is a wonderful commenting system that increases user commets, engagement and interactivity.

Unfortunately, like most other plugins, it does some annoying things that can cause site speed issues, or in my case, minification issues.

In this post, I am assuming that you are using DISQUS comments and their respective WordPress plugin to output and manage those comments.

While in the process of redesigning and redeveloping my site, I kept running into a persistent issue of minify errors on my pages. I traced it down to minifying inline JavaScript, which was incredibly perplexing because I hadn’t inserted any inline JavaScript in my pages at the time. After some more digging, I realized that the script was coming from DISQUS and was loading on every single page of my site – yikes!

Taming DISQUS

Because I’m a performance nut, I decided to figure out how I could resolve this issue because I only load comments on single blog posts. After hunting around in the admin, I couldn’t find any options or settings that would let me determine what pages the script should load. So what did I do? What any good WordPress developer would do – dig into the problem and find the source of the problem!

After hunting in the plugin, I noticed to hooks that were being used liberally without any checks: loop_end and wp_footer. Specifically, the code looked like this:

Part of me got excited because I didn’t even know the loop_end hook existed, but the other part of me shrugged because they had taken no care to check where the code should be loaded. To be fair to them, they are targeting a massive audience that may use comments on posts, pages and other post types. Even still, you could at least check and *not* output your code on archive pages. I digress.

Anyways, there was a simple solution to the problem: determine where I wanted the code to load and remove it from everywhere else. So that’s exactly what I did, and I hope you can benefit from it too.

I only load comments on single posts, so I wanted to ensure I only loaded the code on single posts. You can take this and utilize it too!

Boom. Now the unbridled JS is only loaded on our single posts. If you want to target just single posts and pages, you could use this code instead:

This code will prevent DISQUS from outputting JS on all areas of your site except for single posts and pages. We can even take this one step further to ensure that comments are actually available on the post or page before we load the code, like this:

So there you go – you can now tame DISQUS comments and make sure that they behave in a way that is performance minded.

Hope you enjoyed the tutorial! :-)

Have you enjoyed reading Tame DISQUS Comments in WordPress? Sign up below to get regular content updates and also get my "7 Tips to a Professional WordPress Setup" absolutely free!

About Thomas Griffin

I live and breathe WordPress. I create products around WordPress (Soliloquy and OptinMonster), contribute to WordPress core and do lots of fun development around WordPress in general. You can find me on Twitter, Facebook and Google+.

  • http://www.brandonkraft.com/ Brandon Kraft

    Very, very nice. I know what I’m dropping into the site after “real” work is finished.

    • griffinjt

      Haha thanks! I totally get what you are saying. ;-)

  • Pingback: Scion Players | Tame DISQUS Comments in WordPress

  • Adam Penton

    Hey Griffin! Thanks for this tutorial – really helps! I am plagued by a problem on my post page. I get the error ‘DISQUS seems to be taking longer than usual. Reload?’ no matter what I do. What could be the problem. You can look for example, in this link: https://mymzone.com/blog/portobello-market-how-to-get-there/ Thanks buddy!

  • http://www.healthinformationtips.net/ health insurance

    Disqus is better for me. I’d say by far the most important reason to use disqus is that discussion on your blog will turn up on friendfeed. This gives exposure to a really influential group of people.

  • http://twitter.com/joey89924 joey

    really influential group of people.
    KA5L0380R

  • http://www.healthinformationtips.net/ health insurance

    We hope Disqus update their commenting system from time to time in order to keep it working well.

  • gujjufunzone

    hmmmmmmmmm

  • http://www.expertvillagemedia.com/ WordPress Developer

    Cool and very help full in WordPress websites.

    Thanks for sharing

  • dylanized

    Dude you just saved me so much time! gracias

  • http://www.jb-webs.com/ martiin

    Wow, I installed it today in my side http://www.jb-webs.com but when I understand you article here correctly, I have to make an update by hand tommorow morning that that realy work. Is that still actual? I just ask because your articel is from July 2013, maybe there is hope that they fixed that alreay … OK, I do it on my to do list – thanks a lot to sharing this here

  • Andrea Ballerino

    Thanks Thomas for you code, works perfectly! ;)

  • brennaob

    Exactly what I was looking for. Thanks!

  • http://boirecords.co.uk/ Nick Moreton

    Thanks so much for this – I had a custom loop for a slider and this was dumping code in to one of the slides! Great fix

  • graphicbeacon

    Thank you Thomas. I recently decided to integrate Disqus into my site revamp so this will be useful.

  • thankyou
  • http://www.insurancepolicy.org.uk InsurancePolicy

    insurance policy information here

  • Kaitlin Jurasek

    Hi, Thanks for the info. Where exactly am I supposed to put this code? I am having issues trying to get Disqus to post separately on each of my blog posts! It just keeps posting them in a long stream from the previous comments. HOW DO I FIX THIS!?!?