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.
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:
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!