Snippet

Integrate Disqus Comment System into Your WordPress Site

How to add disqus to wordpressInstall disqus comment system wordpressDisqus plugin setup wordpressIntegrate disqus comments wordpressDisqus comment system wordpress tutorialUse disqus for wordpress commentsDisqus installation guide wordpressReplace wordpress comments with disqusDisqus comment system wordpress integrationSetup disqus on wordpress site

Explanation

To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.

Here's what the code does:

  • It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.
  • You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.
  • The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.
  • It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.

Once you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments.

Code

1// Function to integrate Disqus comment system into WordPress 2function wp_dudecom_add_disqus_comments() { 3 // Check if we are on a single post or page 4 if (is_single() || is_page()) { 5 // Replace the default WordPress comments template with Disqus 6 remove_filter('comments_template', 'wp_comments_template'); 7 add_filter('comments_template', 'wp_dudecom_disqus_comments_template'); 8 } 9} 10 11// Custom function to load Disqus comments template 12function wp_dudecom_disqus_comments_template($comment_template) { 13 // Ensure Disqus shortname is set 14 $disqus_shortname = 'your-disqus-shortname'; // Replace with your Disqus shortname 15 16 // Output Disqus embed code 17 echo '<div id="disqus_thread"></div>'; 18 echo '<script>'; 19 echo 'var disqus_config = function () {'; 20 echo 'this.page.url = "' . get_permalink() . '";'; // Set the page URL 21 echo 'this.page.identifier = "' . get_the_ID() . '";'; // Set the page identifier 22 echo '};'; 23 echo '(function() {'; // Load the Disqus embed script 24 echo 'var d = document, s = d.createElement("script");'; 25 echo 's.src = "https://' . $disqus_shortname . '.disqus.com/embed.js";'; 26 echo 's.setAttribute("data-timestamp", +new Date());'; 27 echo '(d.head || d.body).appendChild(s);'; 28 echo '})();'; 29 echo '</script>'; 30 echo '<noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>'; 31 32 // Return an empty string to prevent loading the default comments template 33 return ''; 34} 35 36// Hook the function to the appropriate WordPress action 37add_action('wp', 'wp_dudecom_add_disqus_comments'); 38 39// Ensure Disqus comments count script is loaded in the footer 40function wp_dudecom_disqus_comments_count_script() { 41 $disqus_shortname = 'your-disqus-shortname'; // Replace with your Disqus shortname 42 echo '<script id="dsq-count-scr" src="https://' . $disqus_shortname . '.disqus.com/count.js" async></script>'; 43} 44 45// Hook the comments count script to wp_footer 46add_action('wp_footer', 'wp_dudecom_disqus_comments_count_script');

Instructions

File Location: Add the following code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Create a Disqus account if you don't have one.
  • Obtain your Disqus shortname from your Disqus account settings.

Implementation Steps:

  1. Open your WordPress admin panel and navigate to Appearance > Theme Editor or use an FTP client to access your theme files.
  2. Locate and open the functions.php file of your active theme.
  3. Copy and paste the provided code into the functions.php file.
  4. Replace 'your-disqus-shortname' with your actual Disqus shortname in the code.
  5. Save the changes to the functions.php file.
  6. Visit a single post or page on your site to ensure Disqus comments are displayed instead of the default WordPress comments.

If you encounter any issues or need further assistance, consider reaching out to wp-dude.com for expert help with implementation or advanced functionality.

';\n echo '';\n\n // Return an empty string to prevent loading the default comments template\n return '';\n}\n\n// Hook the function to the appropriate WordPress action\nadd_action('wp', 'wp_dudecom_add_disqus_comments');\n\n// Ensure Disqus comments count script is loaded in the footer\nfunction wp_dudecom_disqus_comments_count_script() {\n $disqus_shortname = 'your-disqus-shortname'; // Replace with your Disqus shortname\n echo '';\n}\n\n// Hook the comments count script to wp_footer\nadd_action('wp_footer', 'wp_dudecom_disqus_comments_count_script');","encodingFormat":"application/x-httpd-php","datePublished":"2024-12-20T15:58:36","dateModified":"2024-12-20T15:58:36","author":{"@type":"Person","name":"123","url":"https://srv106014.seohost.com.pl"},"keywords":"External Integrations"},{"@type":"HowTo","@id":"https://wp-dude.com/code-snippet/integrate-disqus-comment-system-wordpress#howto","name":"Integrate Disqus Comment System into Your WordPress Site – instrukcja","description":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments.","step":[{"@type":"HowToStep","text":"Create a Disqus account if you don't have one."},{"@type":"HowToStep","text":"Obtain your Disqus shortname from your Disqus account settings."},{"@type":"HowToStep","text":"Open your WordPress admin panel and navigate to Appearance > Theme Editor or use an FTP client to access your theme files."},{"@type":"HowToStep","text":"Locate and open the functions.php file of your active theme."},{"@type":"HowToStep","text":"Copy and paste the provided code into the functions.php file."},{"@type":"HowToStep","text":"Replace 'your-disqus-shortname' with your actual Disqus shortname in the code."},{"@type":"HowToStep","text":"Save the changes to the functions.php file."},{"@type":"HowToStep","text":"Visit a single post or page on your site to ensure Disqus comments are displayed instead of the default WordPress comments."},{"@type":"HowToStep","name":"Kod (PHP)","text":"// Function to integrate Disqus comment system into WordPress\nfunction wp_dudecom_add_disqus_comments() {\n // Check if we are on a single post or page\n if (is_single() || is_page()) {\n // Replace the default WordPress comments template with Disqus\n remove_filter('comments_template', 'wp_comments_template');\n add_filter('comments_template', 'wp_dudecom_disqus_comments_template');\n }\n}\n\n// Custom function to load Disqus comments template\nfunction wp_dudecom_disqus_comments_template($comment_template) {\n // Ensure Disqus shortname is set\n $disqus_shortname = 'your-disqus-shortname'; // Replace with your Disqus shortname\n\n // Output Disqus embed code\n echo '
';\n echo '';\n echo '';\n\n // Return an empty string to prevent loading the default comments template\n return '';\n}\n\n// Hook the function to the appropriate WordPress action\nadd_action('wp', 'wp_dudecom_add_disqus_comments');\n\n// Ensure Disqus comments count script is loaded in the footer\nfunction wp_dudecom_disqus_comments_count_script() {\n $disqus_shortname = 'your-disqus-shortname'; // Replace with your Disqus shortname\n echo '';\n}\n\n// Hook the comments count script to wp_footer\nadd_action('wp_footer', 'wp_dudecom_disqus_comments_count_script');"}]},{"@type":"FAQPage","@id":"https://wp-dude.com/code-snippet/integrate-disqus-comment-system-wordpress#faq","mainEntity":[{"@type":"Question","name":"how to add disqus to wordpress","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"install disqus comment system wordpress","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"disqus plugin setup wordpress","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"integrate disqus comments wordpress","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"disqus comment system wordpress tutorial","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"use disqus for wordpress comments","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"disqus installation guide wordpress","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"replace wordpress comments with disqus","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"disqus comment system wordpress integration","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}},{"@type":"Question","name":"setup disqus on wordpress site","acceptedAnswer":{"@type":"Answer","text":"To swap out the default WordPress comment system with Disqus, you'll need to follow a few simple steps. This code helps you do just that by integrating Disqus seamlessly into your WordPress site.\n\nHere's what the code does:\n\n\n It checks if you're on a single post or page. If you are, it replaces the default WordPress comments with Disqus.\n You'll need to replace 'your-disqus-shortname' with your actual Disqus shortname. This is a unique identifier for your Disqus account.\n The code sets up Disqus to load on your pages by embedding a script that pulls in the Disqus comment system.\n It also ensures that the Disqus comment count script is loaded in the footer of your site, which helps display the number of comments on your posts.\n\n\nOnce you've added this code, Disqus will handle all your commenting needs, providing a more interactive and engaging experience for your visitors. Just make sure JavaScript is enabled in your visitors' browsers, as Disqus relies on it to display comments."}}]}]}