How To Add Recent Comments Widget Using Disqus API

Disqus is a simple-to-install comments widget for websites, blogs and online community sites. It has some really awesome features like, social network integration, advanced administration and moderation options, and some other extended community functions.

In this tutorial you will learn to add a recent comments widget to your site using the Disqus API. So without wasting any time, lets just start with the tutorial.

Prerequisites

There are a few things you need to do before moving forward.

1. Register a New Application

To register a new application, sign in to your Disqus account and follow the steps below.

  • Go to Applications under API Resources.
  • Click on Register new application and fill in the form to register the application.

2. Get the API Key

Once the application is registered, go to the application’s details page and scroll down to OAuth Settings. Now copy the API Key and save it somewhere.

3. Get the Forum Name

To get the forum name, go to Admin > Settings and scroll down to Site Identity, you will find the Shortname which is going to be your forum name.

Include DQRecentComments.php

Download the DQRecentComments.zip file and unzip it. It contains DQRecentComments.php file that has some functions which will help you get the most recent comments.

File: DQRecentComments.zip

To include the file, you can use the require_once() function as follow.

Code:

<?php
// Include the DQRecentComments.php file.
require_once("DQRecentComments.php");
?>

Use DQGetRecentComments() Function

Now call the DQGetRecentComments() function to get the recent comments. All you need to do is pass on some parameters as an array. The rest it will do it for you and will return an array containing information about the recent comments on your site. If the request fails some how, DQGetRecentComments() function will return FALSE.

Required Parameters

Following are the required parameter, otherwise the function will return FALSE.

  • APIKey: This is the API Key of the application that you registered earlier.
  • forumName: The forum name is the Shortname that you found under Site Identity.

Optional Parameters

Following are the optional parameters that you can define, otherwise default values will be used.

  • commentCount: The number of comments you want to show. The default value is 25.
  • commentLength: The maximum length of the comments. The default value is 100.

Following code example shows how you can set the parameters as an array and pass it on to the DQGetRecentComments() function.

Code:

<?php
// Make an array for the parameters.
$parameters = array(
'APIKey' => 'YOUR_APPLICATION_API_KEY',
'forumName' => 'YOUR_FORUM_NAME',
'commentCount' => 10,
'commentLength' => 95
);
// Using the DQGetRecentComments() function.
$DQComments = DQGetRecentComments($parameters);
?>

The DQGetRecentComments() function does all the work for you, i.e. making the request using the Disqus API and returning the response as an array. You can use the print_r() function to see the returned array as follow.

Code:

<?php
// Make an array for the parameters.
$parameters = array(
'APIKey' => 'YOUR_APPLICATION_API_KEY',
'forumName' => 'YOUR_FORUM_NAME',
'commentCount' => 2,
'commentLength' => 95
);
// Using the DQGetRecentComments() function.
$DQComments = DQGetRecentComments($parameters);
// Using print_r() function to see the returned array.
print_r($DQComments);
?>

Output:

Array
(
[0] => Array
(
[createdAt] => 2013-04-19T12:48:51
[message] => 'This is the comment.'
[pageTitle] => 'Title of the page.'
[pageURL] => 'URL of the page.'
[authorName] => 'Name of comment author.'
[authorProfileURL] => 'Profile URL of comment author.'
[authorAvatar] => 'Image URL of comment author.'
)
[1] => Array
(
[createdAt] => 2013-04-15T18:39:47
[message] => 'This is the comment.'
[pageTitle] => 'Title of the page.'
[pageURL] => 'URL of the page.'
[authorName] => 'Name of comment author.'
[authorProfileURL] => 'Profile URL of comment author.'
[authorAvatar] => 'Image URL of comment author.'
)
)

The above output shows a sample returned array of the DQGetRecentComments() function that has two recent comments. Now you can use the for or foreach loop to use traverse through the array and echo it with your desired HTML and CSS.