Stop Disqus JavaScript on All WordPress Pages Except Posts or Pages

x theme

If you’re using the Disqus commenting system for your posts and pages. You might be worried about the JavaScript output by Disqus which is caused in slow speed of the website. The logic is that, you’re using Disqus on your posts and pages, then why you have to load Disqus JavaScript on every page.

Now this is the time to stop Disqus JS on all pages except posts or pages. The following code will execute JavaScript on the posts or pages where comments are allowed.

add_action( 'wp_head', 'tgm_tame_disqus_comments' );
function tgm_tame_disqus_comments() {
if ( is_singular( array( 'post', 'page' ) ) && comments_open() )
return;
remove_action( 'loop_end', 'dsq_loop_end' );
remove_action( 'wp_footer', 'dsq_output_footer_comment_js' );
}

Thanks to thomasgriffin for this code.

Leave a Reply

Your email address will not be published. Required fields are marked *