Back to Top

Better WordPress Recent Comments

Better WordPress Recent Comments

This plugin displays recent comment lists at assigned locations. Comment lists are updated on the fly when a visitor adds a comment or when you moderate one. Does not add any load for end-users.

What could a blog be without comments from visitors? I myself value comments as much as I value my clients, because through comments, you know what went well, and what went wrong. A recent comment list, in my opinion, can help stimulate discussion and exploration of your blog tremendously.

Now for the past few months I have been using a plugin called Get Recent Comments; though this plugin is configurable and indeed popular, the code is somehow messy and no support for custom post type is found. The worst thing is Get Recent Comment doesn’t seem to be updated anymore, so I decide to write another recent comment plugin which is more lightweight and makes use of some nice features provided by WordPress 3.0.

Plugin Features

  • Has the options to show comment only, trackback only, or show both (separately or all together)
  • Possibility to get comments from specific post/page/custom post types
  • Possibility to add different comment lists with different settings on one page
  • Possibility to group comments by posts (1.2.0 new!)
  • You can show comments on a separate page (using template function or shortcode) with pagination, and of course, separate template!
  • You can sort comment lists descendingly or ascendingly
  • Navigate through comments using AJAX or non-AJAX navigation (1.2.0 new!)
  • Supports custom post type
  • Supports Gravatar
  • Supports smiley
  • Widget-ready
  • Template functions ready
  • Generate Zero SQL query for end-users
  • Possibility to trim comment to a specific number of words
  • Possibility to trim post title to a specific number of words (1.1.0)
  • Possibility to split long words into smaller chunks
  • Configurable date format
  • Possibility to hide comments by you and/or trackbacks/pingbacks that originate from your website
  • Style the comment list using your own CSS or using the default one. As of 1.2.0 the plugin will use a different CSS file if you disable avatars.
  • Option to load media files (CSS, JS, etc.) only when needed
  • Different template for comment and trackback. An additional template for blog owner is available.
  • WordPress Multi-site compatible (not tested with WPMU)
  • Actively developed
  • And more…

Plugin Usage

Basic Usage

If you do not need any special recent comment list then showing one using this plugin is very easy. For WordPress 2.9 or higher, you can use the widget for widget-ready themes (ignore all the inputs for now):

BWP Recent Comment Widget

BWP Recent Comment Widget

or you can use template functions, which have the same functionality:

  1. bwp_get_recent_comments(); // get recent comments based on options in the setting page
  2. bwp_get_recent_trackbacks(); // same as above, except it will get recent trackbacks/pingbacks only
bwp_get_recent_comments(); // get recent comments based on options in the setting page
bwp_get_recent_trackbacks(); // same as above, except it will get recent trackbacks/pingbacks only

Keep in mind that those functions and widgets will generate a Global comment list (let’s call it a Global List) based on options in the setting page.

For example, if you choose “Show all comment types together”, the comment list you receive will be a mix of comments and trackbacks and the total number of items returned will be determined by the number of comments you want to show at most in the setting page. If you choose “Show all comment types, but separately”, comments and trackbacks will be separated, and each will have a different number of items.

If you use bwp_get_recent_trackbacks();, you will get a list of recent trackbacks only.

Advanced Usage

If you want to have more complex comment lists, this section is for you. Let’s talk about the advanced use of widgets first. Uncheck the box titled “Use save settings for this instance”, you will get:

BWP Recent Comments - Advanced Widget Usage

BWP Recent Comments – Advanced Widget Usage

With the Instance Name being empty, you are telling the plugin to generate a Global List (explanation above).

The Instance Settings panel allows you to dynamically output your comment list. For example, you would like to show two widgets in your sidebar, one to show comments and one to show trackbacks/pingbacks. Using the Global List, just choose to separate comments and trackbacks in the setting page, and then change the comment type of each widget to its desired comment type, and you’re done! Zero database load? Yes.

You can also conveniently change the order of comments if you wish; for example, you can have the comment list showing newer ones first while the trackback list showing older ones first. Isn’t that dynamic?

For a Global List, you can not apply post type or change comment limit or change separation behaviour, those options are controlled by what you choose in the setting page. The Global List will get comments from all post types (that’s why I use the adjective Global), so if you want to get comments from a specific post type, you will have to create a Local List.

A Local List is identical to the Global List except for the ability to define its own settings. It will use the settings you choose in the Instance Settings box above and use the Global List’s settings for fields you leave out (except for the ‘separation’ and ‘ajax navigation’ checkboxes).

To set a Local List, you will need an Instance Name, each Local List is identified by its Instance Name. The Instance Name must be unique and can be as simple as a post type’s label, and can only contain alphanumeric characters, plus hyphens (-), underscores ( _ ) and spaces.

For example you would like to have a recent comment list of comments from your pages only, you can have an instance name of “page” and then choose Pages as your Post Type. Or you may want to pull comments from a specific post, simply type in that post’s ID or postname (slug).

If you want to separate comments and trackbacks for the newly created instance, you will have to check “Separate comments and trackbacks” because this field will not take value from the Global List (the same goes for AJAX navigation setting). Any other settings should be self-explanatory.

“Use saved settings for this instance”?

When you create a new widget that has the same instance name as another widget, you can copy all settings from the previous widget to the new one by checking that checkbox and then press Save. After that you can change some settings if you wish, the process should be just like with the Global List.

Keep in mind that you should have the group limit, post type, post ID or postname and the comment and trackback limit staying the same for all widgets with the same instance name, otherwise this plugin will try to refresh the instance every time it is loaded, which is not a good thing at all.

Remember, it’s best to write the instance name, then press Save first before you change anything, this ensures that all instances are created correctly and no SQL queries would be unnecessarily generated.

Using template functions instead of widgets

Now if you want to use template functions instead, all you have to do is to call the function bwp_get_recent_comments(); with an array consisting of the same settings you would have when using widget. The structure of the array should be:

  1. $args = array(
  2.     'instance'     => 'instance name',
  3.     'post_type'    => 'post type name',
  4.     'post_id'      => '', // ID or slug
  5.     'grouped'      => 0, // limit the number of grouped comments per post, 0 to disable
  6.     'comment_type' => 'which type of comment to show', // 'all', 'comment', or 'tb'
  7.     'separate'     => true or false,
  8.     'ajax'         => true or false, // enable AJAX navigation
  9.     'limit'        => 0, // number of comments to show
  10.     'tb_limit'     => 0, // number of trackbacks/pingbacks to show
  11.     'order'        => 'asc' or 'desc'
  12. );
$args = array(
	'instance'     => 'instance name',
	'post_type'    => 'post type name',
	'post_id'      => '', // ID or slug
	'grouped'      => 0, // limit the number of grouped comments per post, 0 to disable
	'comment_type' => 'which type of comment to show', // 'all', 'comment', or 'tb'
	'separate'     => true or false,
	'ajax'         => true or false, // enable AJAX navigation
	'limit'        => 0, // number of comments to show
	'tb_limit'     => 0, // number of trackbacks/pingbacks to show
	'order'        => 'asc' or 'desc'
);

For example, this will allow you to create a recent comment instance that has an instance name of ‘page’ with comments and trackbacks separated:

  1. bwp_get_recent_comments(array('instance' => 'page', 'separate' => true, 'post_type' => 'page'));
bwp_get_recent_comments(array('instance' => 'page', 'separate' => true, 'post_type' => 'page'));

Show two separate HTML list of comments and trackbacks:

  1. <h3>Recent Comments</h3>
  2. <ul>
  3.     <?php bwp_get_recent_comments(array('instance' => 'page', 'separate' => true, 'post_type' => 'page', 'comment_type' => 'comment')); ?>
  4. </ul>
  5. <h3>Recent Trackbacks</h3>
  6. <ul>
  7.     <?php bwp_get_recent_comments(array('instance' => 'page', 'comment_type' => 'tb')); ?>
  8. </ul>
<h3>Recent Comments</h3>
<ul>
	<?php bwp_get_recent_comments(array('instance' => 'page', 'separate' => true, 'post_type' => 'page', 'comment_type' => 'comment')); ?>
</ul>
<h3>Recent Trackbacks</h3>
<ul>
	<?php bwp_get_recent_comments(array('instance' => 'page', 'comment_type' => 'tb')); ?>
</ul>

Notice how the second function is set up: you identify it with the instance name and then only change what’s needed (in the above case, only comment_type was changed). As stated, you should never change ‘limit’ or ‘tb_limit’ or ‘post_type’ or ‘post_id’ or ‘grouped’ in the second function as it will tell the plugin to refresh the instance, generating SQL queries for every page load.

Whenever you feel that the comment list is displaying something wrong, simply visit the setting page to refresh all current instances. You can also open the Instance tab in the setting page to delete any instance you do not use any more, saving some space for your table.

AJAX navigation

As described above, you can enable AJAX navigation for your recent comment lists easily using both widgets and template functions. Below you will see it in action on the default theme of WordPress 3.2:

BWP Recent Comments AJAX Navigation

BWP Recent Comments AJAX Navigation

Comments on a separate page with pagination

Since 1.1.0, you will be able to show recent comments on a separate page with pagination as well as custom template. This is particularly useful to people who want a GuestBook page or similar things.

The easiest way to use this feature is to use this plugin’s shortcode (available since 1.2.0), like so:

[bwp-rc]

The above code will effectively show the Global List on that selected page, with pagination.

To control how the shortcode functions, use the following attributes:

'instance' // the instance to get comments from
'ajax' // whether to enable AJAX navigation or not (set to "yes")
'limit' // comment limit for each page
'prev' // "previous" text, default to '&laquo;&laquo;'
'next' // "next" text default to '&raquo;&raquo;'

If you want to use a local list (such as ‘guestbook’) with AJAX navigation, you should first create a new widget, enable AJAX for it, remove the widget, and then use this:

[bwp-rc instance="guestbook" ajax=true]

All other settings such as ‘post type’, ‘grouped’, etc. that you set through the widget will also be used by the shortcode.

If you do not like to use shortcode, you can use a template function to show comments on a separate page. Open a singular theme file, such as page.php, single.php, etc. and place the following lines at the position where you want the comment list to appear (the CSS class is important if you want to use AJAX):

  1. <ul class="bwp-rc-ulist">
  2. <?php if (function_exists('bwp_rc_paged_list')) bwp_rc_paged_list(); ?>
  3. </ul>
<ul class="bwp-rc-ulist">
<?php if (function_exists('bwp_rc_paged_list')) bwp_rc_paged_list(); ?>
</ul>

That’s it! And here’s what the above function would produce:

BWP Recent Comments on a Separate Page

BWP Recent Comments on a Separate Page

To show fewer or more comments per page, simply change that function to:

  1. if (function_exists('bwp_rc_paged_list'))
  2.     // Show only 3 comments per page
  3.     bwp_rc_paged_list(array(), 3);
if (function_exists('bwp_rc_paged_list'))
	// Show only 3 comments per page
	bwp_rc_paged_list(array(), 3);

and if you would like to change the next and previous links, try this:

  1. if (function_exists('bwp_rc_paged_list'))
  2.     bwp_rc_paged_list(array(), 10, 'Previous', 'Next');
if (function_exists('bwp_rc_paged_list'))
	bwp_rc_paged_list(array(), 10, 'Previous', 'Next');

The template function can be customized even more just like the shortcode. For example you can also have something like this:

  1. if (function_exists('bwp_rc_paged_list'))
  2.     // Show 10 comments per page
  3.     // Use instance named 'guestbook', with AJAX
  4.     bwp_rc_paged_list(array('instance' => 'guestbook', 'ajax' => true), 10);
if (function_exists('bwp_rc_paged_list'))
	// Show 10 comments per page
	// Use instance named 'guestbook', with AJAX
	bwp_rc_paged_list(array('instance' => 'guestbook', 'ajax' => true), 10);

Most arguments are also available, such as ‘post_type’, and post_id’, but the rest will not work properly, as we are paginating all (real) comments here. That’s right, no pingback or trackback allowed.

Grouped comments by posts

As of version 1.2.0, you will be able to group recent comments by posts. A grouped comment list can be shown on a separate page if you want, and can even have AJAX navigation appended to it. Below is a list with grouped comments on a page:

BWP Recent Comments Grouped on a Page

BWP Recent Comments Grouped on a Page

All comments are sorted by the date they were posted, and no post will have more comments than what you set in the widget or the template function.

Customization

Probably the only thing you would like to customize in this plugin is the CSS. Simply disable the CSS provided by this plugin and add your own CSS rules to any CSS file you find suitable.

Also, if you use your own avatar instead of the default one you choose in Settings → Discussion, use the below filter to apply it to BWP RC:

  1. add_filter('bwp_rc_default_avatar', 'my_comment_avatar');
  2. function my_comment_avatar()
  3. {
  4.     return 'http://example.com/avatar.gif';
  5. }
add_filter('bwp_rc_default_avatar', 'my_comment_avatar');
function my_comment_avatar()
{
	return 'http://example.com/avatar.gif';
}

Templates for comments and trackbacks can be edited in the Template tab of the setting page. If you do not like what you have, you can press reset to start from the beginning. Note that the reset button will not save your template, the idea is you can look at the default template again without losing your current template. Things are saved only when you press Save Changes. The template for blog’s owner (that’s you) is optional and will be used if not empty.

Important Notes

This plugin does not come with category-based comment list, simply because I don’t really find a use for it. If there would be different types of comment lists, they should be post type based. A post type somehow indicates a different section of a website and therefore deserves a different comment list.

To-do List

  • Add support for multiple post types (1.3.0)
  • Add support for page exclusion (1.3.0)
  • Add support for relative time format (e.g. 15 mins ago) (1.3.0)
  • Add support for HTML comment excerpt (1.3.0)
  • Add more per-instance settings (1.3.0)
  • Add support for category exclusion (1.3.0)
  • Improve Ajax recent comment list (more options – 1.3.0)
  • Improve comment text for protected posts (more options – 1.3.0)
  • Improve Templates (more options for avatar – 1.3.0)
  • Auto-refresh comment list (good for using with page cache plugins – 1.3.0)

Hook References

  • bwp_rc_date_format – Used to apply advanced date formatting, such as x days ago, etc. (filter)
  • bwp_rc_before_pagination – Used to show other necessary markups before the non-AJAX navigation for a separate page is shown. (filter)
  • bwp_rc_default_avatar – Used to change the default avatar set by WordPress. (filter)
  • bwp_rc_dotdotdot – Used to change the text shown after a trimmed comment (1.2.1 filter).

Contribute to this Plugin

This plugin is licensed under GPL version 3, and it needs contributions from the community.

Buy me some special coffees!

My plugins and support for them are free. If you like my work and could buy me some (special) coffees, I would be much appreciated! They might help with some overnight times debugging my plugins, you know.

Support, Feedback, and Code Improvement

i18n (Translate the plugin)

If you are a translator, please help translating this plugin. Even if you aren't, you can become one, it is very easy and fun! If you want to know how, please read here: Create a .pot or .po File using Poedit.

Print Article Watch Log