{"id":41695,"date":"2014-08-11T10:21:55","date_gmt":"2014-08-11T04:51:55","guid":{"rendered":"https:\/\/www.quickonlinetips.com\/archives\/?p=41695"},"modified":"2020-05-24T11:06:55","modified_gmt":"2020-05-24T05:36:55","slug":"load-disqus-comments-on-click-demand","status":"publish","type":"post","link":"https:\/\/www.quickonlinetips.com\/archives\/2014\/08\/load-disqus-comments-on-click-demand\/","title":{"rendered":"How to Load Disqus Comments on Click with jQuery in 8 Easy Steps"},"content":{"rendered":"<p>How to load Disqus comment on click and on-demand using jQuery? You might want to increase page speed or hide comments to save space to display other interactive elements to increase pageviews. You need to see if this works better for you than opening <a title=\"How to Add Popup Comments in WordPress Posts\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2014\/06\/add-popup-comments-in-wordpress-posts\/\">WordPress popup comments<\/a> or loading <a title=\"How to Show WordPress Comments on New Page\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2014\/06\/wordpress-comments-on-new-page\/\">WordPress comments on new page<\/a>. Disqus is a popular WordPress commenting system and you may like to install this is this way.<\/p>\n<h2>Load Disqus Comments on Click<\/h2>\n<p>If you use the power of jQuery and add that to the Universal Disqus embed, you can achieve this is minutes.<\/p>\n<p><img decoding=\"async\" class=\"border alignnone wp-image-41700 size-full\" src=\"https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/load-disqus.png\" alt=\"load disqus comments\" width=\"442\" height=\"390\" border=\"1\" srcset=\"https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/load-disqus.png 442w, https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/load-disqus-150x132.png 150w, https:\/\/www.quickonlinetips.com\/archives\/wp-content\/uploads\/load-disqus-300x264.png 300w\" sizes=\"(max-width: 442px) 100vw, 442px\" \/><\/p>\n<h3>1. Remove Comments template<\/h3>\n<p>First, you need to remove the default comments code from the single page template (single.php). Now Disqus will not automatically replace your WordPress comments.<\/p>\n<p><code>&lt;?php comments_template('', true); ?&gt;<\/code><\/p>\n<h3>2. Manually Add Disqus Comments<\/h3>\n<p>Now you need to manually add the Disqus code to your WordPress theme template. You will need the <a href=\"https:\/\/help.disqus.com\/customer\/portal\/articles\/472097-universal-embed-code\" target=\"_blank\" rel=\"noopener noreferrer\">Disqus universal embed code<\/a>\u00a0which can be added to any website anywhere in the HTML where you wish the comments to appear. Remember to edit it to your Disqus shortname to get started. The script looks like this and is officially provided by Disqus.<\/p>\n<pre><code>&lt;script type=\"text\/javascript\"&gt;\r\n\/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * *\/\r\nvar disqus_shortname = '&lt;example&gt;'; \/\/ Required - Replace example with your forum shortname\r\n\r\n\/* * * DON'T EDIT BELOW THIS LINE * * *\/\r\n(function() {\r\nvar dsq = document.createElement('script'); dsq.type = 'text\/javascript'; dsq.async = true;\r\ndsq.src = '\/\/' + disqus_shortname + '.disqus.com\/embed.js';\r\n(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\r\n})();\r\n&lt;\/script&gt;<\/code><\/pre>\n<h3>3. Edit Comments PHP file<\/h3>\n<p>Add this code to a new PHP file which you can add to the theme template (Learn how to <a title=\"How to Create WordPress Theme Files Without FTP\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2014\/06\/wordpress-theme-files-without-ftp\/\">create PHP files in WordPress without FTP<\/a>). Then add this universal code to this file &#8230; let us call it <em>newcomments.php.<\/em><\/p>\n<h3>4. Load jQuery<\/h3>\n<p>Now load jquery to the footer of the template. Best to add the <a title=\"Remove WordPress jQuery, Add Google CDN jQuery\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2013\/11\/wordpress-jquery-to-google-hosted-jquery\/\">jquery CDN sources<\/a> like those from Google for superfast load times.<\/p>\n<p><code>&lt;script src=\"\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.1\/jquery.min.js\"&gt;&lt;\/script&gt;<\/code><\/p>\n<h3>5. Create Comments button<\/h3>\n<p>Now create a button that needs to be clicked to load comments. Here is the button code (beautifully made with <a title=\"How to Add Icon Font Awesome to Any Site\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2013\/07\/add-icon-font-awesome\/\">font awesome<\/a> and <a title=\"Use Twitter Bootstrap for Powerful Web Design\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2013\/07\/twitter-bootstrap\/\">Bootstrap<\/a>).<\/p>\n<p><code>&lt;div id=\"comment-button\" class=\"btn btn-success\" \/&gt;&lt;i class=\"fa fa-comments\"&gt;&lt;\/i&gt; Comments&lt;\/div&gt;<\/code><\/p>\n<h3>6. Load Comments<\/h3>\n<p>Choose the site where you want to load comments. Add the code which will load comments when the click happens.<\/p>\n<p><code>&lt;div id=\"disqus_thread\"&gt;&lt;\/div&gt;<\/code><\/p>\n<h3>7. Load comments on Click<\/h3>\n<p>Now you need to add the Jquery magic to the button so that when the user clicks the button, the jquery will load the <em>newcomments.php<\/em> on this page. Here is the code, you can add <em>after<\/em> loading the jquery in the footer.<\/p>\n<pre><code>&lt;script type=\"text\/javascript\" language=\"javascript\"&gt;\r\n$(document).ready(function() {\r\n$(\"#comment-button\").click(function(event){\r\n$('#disqus_thread').load('PATH_TO\/loadcomments.php');\r\n});\r\n});\r\n&lt;\/script&gt;<\/code><\/pre>\n<p>Remember to fix the path to where the file is called. That&#8217;s it.<\/p>\n<h3>8. Activate Disqus WP Plugin<\/h3>\n<p>Now activate your <a href=\"https:\/\/wordpress.org\/plugins\/disqus-comment-system\/\" target=\"_blank\" rel=\"noopener noreferrer\">Disqus WordPress plugin<\/a> and it will provide integration with your Disqus comments &#8211; and will load on demand and on clicking the button. Sometimes if you have thousands of comments, it is better to export WordPress comments as WXR files in batches and then import. Learn how to<a title=\"How to Fix WordPress Export, Disqus Import Comments Errors\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2012\/08\/wordpress-disqus-import-export-comments-errors\/\"> fix Disqus import errors<\/a>. This will make your page load faster and load comments only when the user needs to see them.<\/p>\n<p><strong><span style=\"color: #ff0000;\">NOTE<\/span>:<\/strong> <strong>This is NOT standard Disqus implementation.<\/strong> This is an experiment on our site for usable site design and testing in progress. Do at your own risk. Disqus always recommends you keep a database backup before activating and using their plugin (we use <a title=\"Get VaultPress Lite: Cheaper WordPress Backup\" href=\"https:\/\/www.quickonlinetips.com\/archives\/2013\/05\/vaultpress-lite-wordpress-backup\/\">Vaultpress<\/a> for this).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to load Disqus comment on click and on-demand using jQuery? You might want to increase page speed or hide&#8230;<\/p>\n","protected":false},"author":4,"featured_media":41700,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[6],"tags":[49,17],"class_list":["post-41695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogging","tag-tutorials","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/posts\/41695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/comments?post=41695"}],"version-history":[{"count":0,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/posts\/41695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/media\/41700"}],"wp:attachment":[{"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/media?parent=41695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/categories?post=41695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.quickonlinetips.com\/archives\/wp-json\/wp\/v2\/tags?post=41695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}