22 Oct 2008, 1:00am
Programming:
by

leave a comment

Adding filtering to the ‘Recent Comments’ WordPress widget

I recently started using WordPress‘s dynamic sidebar functionality for the side column on this blog which contains things like lists of recent posts, recent comments etc.  My main motivation for doing this was to get access to the ‘Recent Comments’ widget so I could list the most recent comments on my posts.

To activate the widget and the dynamic sidebar I did the following:

  1. Logged into the WordPress admin dashboard.
  2. Clicked on the ‘Design’ tab.
  3. Clicked on the ‘Widgets’ sub tab.
  4. Added a sidebar.
  5. Enabled the ‘Recent Comments’ widget.
  6. Clicked save changes.
  7. Reloaded my blog’s main page in another browser tab and noted the normal side bar had disappeared to be replaced by the sidebar that currently contains only the ‘Recent Comments’ widget.
  8. I then added some other widgets to replicate the original functionality in my original sidebar.

I then noticed that my ‘Recent Comments’ widget contained not only user comments but trackbacks and pingbacks too, this was annoying as I only want user comments to be listed and nothing else.  After some searching around the web for a simple solution,  followed by some experimental tweaking of the WordPress source code, I can now present the simplest solution I could make to filtering out all non-comment comments from the ‘Recent Comments’ widget:

  1. Open your WordPress installation folder via ftp or locally.
  2. Open the ‘wp-includes’ directory.
  3. Make a back up copy of the ‘widgets.php’ file, just in case.
  4. Open the ‘widgets.php’ file.
  5. Search for the following line in the function ‘wp_widget_recent_comments’ (it was at line 981 in version 2.62 of WordPress): $comments = $wpdb->get_results(“SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = ’1′ ORDER BY comment_date_gmt DESC LIMIT $number”);
  6. Make the following addition (in red) to the line: $comments = $wpdb->get_results(“SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = ’1′ AND comment_type = ” ORDER BY comment_date_gmt DESC LIMIT $number”);
  7. Save the modified file.
  8. Upload the updated file to your web server.
  9. Reload your blog’s main page, you should now only see comments in the ‘recent comments’ list.

For those that are interested in what this change actually does, here is an explanation.  The line of php code we are changing queries SQL database that is part of your WordPress installation and searches the comments table for the latest user specified number of approved comments.  The addition we are making adds an extra clause to the SQL query that searches for the latest approved comments: this extra clause specifies that we are only interested in comments when the comments table’s comment_type column has the value ” which is the value WordPress uses to designate user comments and not trackbacks or pingbacks.

Note: This change was made and tested with WordPress version 2.6.2, I don’t know how well it will work with older versions of WordPress.  Also if you upgrade your WordPress installation you will need to make the above change again, unless the new version already implements it.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>