Plugin: Top/Recent Commenters

Name:
Top/Recent Commenters
Author:
Scott Reilly
Version:
1.0
Last updated:
24 March 2005
Download:
Description:

Retrieve the top commenters or most recent commenters to your site (if called outside “the loop”) or for a particular post (if called inside “the loop”).

Notes:

After having just yesterday released a Recent Commenters plugin, I’ve deprecated it and rolled it into this new plugin since Top Commenters functionality only requires slight changes to the code for the previous plugin. And rather than maintain two slightly different plugins, I merged them.

This plugin outputs a list of the N top commenting visitors (with their total comments count) or the most recently commenting authors (with hyperlinks to their homepage if they provided an URL). You can define $before and $after text/HTML to bookend each list item. Specify $num_people to control how many people to list.

  • You can infer the unique identity for your commenters in one of three ways, based on the name they provided, email, or url. By default, this plugin assumes it is based on ‘comment_author_email’. Other options are ‘comment_author_name’ and ‘comment_author_url’. Look at the code for the function for where $identity_field is set, and change the value there if you wish.
  • You can omit users from the listing by setting the function variable $exlude_from_listing to a value appropriate to the $identity_field, such that if you have:

    $identity_field = 'comment_author_email';


    Then you would exclude a person by doing:

    $exclude_from_listing = array('[email protected]');


    Or exclude a set of people:

    $exclude_from_listing = array('[email protected]', '[email protected]');

  • You can also configure whether you want the generated links to the commenter’s sites should be opened in a new window or not:

    $open_in_new_window = false;

  • Can be used inside or outside “the loop”
Installation:
  1. Download the file get-commenters.zip and unzip it into your /wp-content/plugins/ directory.
    -OR-
    Copy and paste the the code ( get-commenters.phps ) into a file called get-commenters.php, and put that file into your /wp-content/plugins/ directory.
  2. Activate the plugin from your WordPress admin ‘Plugins’ page.
  3. Optional: Look at the code for the 3 configuration options you can change —
    a.) You can determine a unique visitor in one of three ways (by name, email, or url); default is email
    b.) If you want to exclude certain people from the listings, you can list them
    c.) You can set whether links to visitor sites open in a new browser window (default is ‘false’)
  4. Add a call to the function to your template (see examples below).
Functions:
[I’ve defined the $num_posts arg as 2 for all of the examples in order to limit the amount of sample output.]

function c2c_get_commenters($type='top', $num_people=5, $before='<li>', $after='</li>', $omit_last_after=false, $do_exclusions=true)

  • $type : Either ‘top’ (to get top commenters) or ‘recent’ (to get recent commenters); default is ‘top’
  • $num_people : The number of recent commenters to be returned; default is 5
  • $before : The text to display before each recent commenter.
  • $after : The text/HTML to display after each recent commenter.
  • $omit_last_after : Boolean value (either ‘true’ or ‘false’) indicating if the $after text should be omitted from the last listed item; default is ‘false’
  • $do_exclusions : Boolean value (either ‘true’ or ‘false’) indicating if the list of people to be excluded (a setting configured within the function itself) should be used; default is ‘true’
Tips & Examples:

1.) Example:

<ul> <?php c2c_get_commenters('top', 3); ?> </ul>

Outputs:

<ul> <li><a href="http://www.joebob.org" title="Visit Joe Bob's site">Joe Bob</a> (75)</li> <li>No Homepage Guy (56)</li> <li><a href="http://www.suzy.org" title="Visit Suzy's site">Suzy</a> (41)</li> </ul>

Which looks like:

2.) Example:

Recent love from: <?php c2c_get_commenters('recent', 3, '', ', ', true); ?>

Outputs:

Recent love from: <a href="http://www.joebob.org" title="Visit Joe Bob's site">Joe Bob</a>, No Homepage Guy, <a href="http://www.suzy.org" title="Visit Suzy's site">Suzy</a>

Which looks like:

Recent love from: Joe Bob, No Homepage Guy, Suzy

Release Log:
  • 24 Mar 2005 : v1.0 —
    • Verified to work for WP 1.5 (still also works for WP 1.2)
    • Fixed bug where “target=” would appear for commenters who did not have URLs
    • Renamed function from get_commenters() to c2c_get_commenters()
    • Moved WP 1.2 compatibility check outside of the primary function.
    • Updated license and examples
  • 06 Dec 2004 : v0.93 — Slight tweak to allow plugin to function in WP1.3+
  • 09 Jul 2004 : v0.92 — Can now define a list of people to exclude from the listings; the exclusion list now applies to both ‘recent’ and ‘top’; the exclusions can be turned of by setting the new $do_exclusions argument to ‘false’ (default is ‘true’); changed the default $identity_field value to ‘comment_author_email’; changed the internal exclusion variable name from $omit_this_identity to $exclude_from_listing
  • 09 Jul 2004 : v0.91 — Fixed bugs reported by kristin at the WordPress Forum. Thanks, kristin!
  • 08 Jul 2004 : v0.9 — Released to the public
Copyright & Disclaimer:

Copyright (c) 2004-2005 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIESOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgements:
I’d like to thank Nick Momrik since I used his Recent Comments and Recent Posts plugins as the initial inspiration for original versions of this plugin.

87 thoughts on “Plugin: Top/Recent Commenters

  1. PLUGIN UPDATED: v0.92

    This plugin has been updated to add a few new features:
    * Can now define a list of people to exclude from the listings
    * The exclusion list now applies to both ‘recent’ and ‘top’
    * The exclusions can be turned of by setting the new $do_exclusions argument to ‘false’ (default is ‘true’)
    * Changed the default $identity_field value to ‘comment_author_email’

    And a small item:
    * Changed the internal exclusion variable name from $omit_this_identity to $exclude_from_listing

  2. Pingback: Code Novice
  3. I downloaded yesterday, installed, activated it, added the tags to my template.. and nuthin. I tried copying and pasting both examples directly to my template as well. I know it has to be something simple I’m overlooking. Any ideas?

  4. Carrie: I had some formatting issues for the sample code shown on this page. Those have been addressed, so try again pasting one of the examples above, for instance:

    <ul> <?php get_commenters('top', 3); ?> </ul>

  5. Hi, I’ve been trying to install this plugin, and it works, but it’s not retrieving all posts or stats when I try to use it outside of the loop. For example, this link, on the top of my posts I’ve included the stats within the loop and they are correct, but outside the loop on the right column they aren’t showing correctly. I was wondering if this had to do with my wp upgrade or if I’m just doing something wrong.. Any ideas?

  6. I moved the code around to different areas of the page and now it’s working with my menu positioned up above my post code. *shrug* I don’t know why this should make a difference, but apparently it does.

  7. Sara: Yeah, there is an issue with using it outside the loop but *after* the loop (the loop == post code). I’ve been looking into it and will continue to do so. Glad you figured it out!

  8. Plugin updated to v0.93

    A slight tweak to allow the plugin to function in WordPress v1.3+.

    Valerie: I haven’t tried this updated version of the plugin in 1.3. If you do, let me know if it works.

    Jauhari: You might have better luck asking for help on wordpress.org/support. Otherwise, at what point in the instructions for installing the plugin are you having problems?

  9. I’ve tried installing it, and have put it in the sidebar. It’s showing two people’s comments. However, there are other comments that it hasn’t counted. I know I have at least one person who has commented twice (this is a new blog), but it won’t show up. What have I done wrong?

  10. Several hours later….The list is changing, and it’s not because more people have commented (no one has). There are now three people on the list, but the top commenter isn’t even on the list anymore. It’s just a completely random change.

  11. Hi there, not sure if this is a wordpress 1.5 thing or whatever, but basically if I take the example you’ve done above, then the comma isn’t seen. Everything else works fine but the comma just doesn’t turn up. Any idea what might be causing this?

  12. Jon: By default the plugin determines unique visitors based on the email they provide. If a commenter uses different e-mail addresses across comments (or if you don’t require commenters to input an e-mail address when they comment) then you can’t trust the plugin’s ability to identify commenters. Are either of these things the case for you? And are you certain that the call is occurring outside “the loop”?

    khaled: Which specific example did you try? I’m guessing the second example. Here it is again with different quoting:

    <?php get_commenters("recent", 3, "", ", ", true); ?>

    Any better luck?

  13. Scott (or someone else that might now),

    I have altered my wpdb structure to include a ‘sticky’ status along with the ‘publish’ status. However the current code only shows comments with the ‘publish’ status. Is there a way to include multiple status request from the wpdb as this line in the code specifies:

    $request = "SELECT ID, comment_ID, comment_content, comment_author FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND <strong><i>post_status = 'publish'</i></strong> ";

    Thanks for any help, Marius

  14. Never mind…I just took out this piece of code and now it works.

    AND post_status = 'publish'

    If someone would like to educate me in this with another operator I would still love to know.

    Thanks, Marius

  15. Marius: Actually, you might want to use this in place of the original code you posted, instead of removing the bit you mentioned in your followup:

    $request = "SELECT ID, comment_ID, comment_content, comment_author FROM $tableposts, $tablecomments WHERE $tableposts.ID=$tablecomments.comment_post_ID AND ( post_status = 'publish' OR post_status = 'sticky' )";

    If you remove the post_status clause from the SQL, you’ll run the risk of getting drafts, static pages, and private posts, which you likely don’t want to have happen.

  16. Thanks Scott…it was the () that I was missing! I implement this now!

    On another note:

    Say you have 2 WP installation A and B.

    Could the code be modified so that the recent comments displayed on A would show both the comments from A and B?

    Just a thought.

  17. Oops….don’t know if this is beyond the spectrum of this script, but please check my site oomsonline.com.

    I recently integrated smileys on my site, but the recent comments is not able to show the smiley but shows ‘:smileycode:’ instead.

    Is there a way to fix this?

    Regards,
    Marius

  18. Marius: Nice site! Anyhow, what “recent comments” and what smiley plugins are you using? Most likely the “recent comments” plugin isn’t applying filters to the comment excerpts, which is necessay for the smiley plugin to put in the image. Should be an easy enough fix, but something I’d have to suggest specifically based on the plugins you are trying to use.

  19. Scott, regarding an earlier comment I had…I am still curious if this plugin could check multiple tables from the database, say if you have multiple installs of WP using one database but multiple tables with different prefixes.

    That way I could show the latest comments of my whole site which includes 4 seperate installs of WP.

    What are your thoughts on this?

    Regards,

    Marius

  20. Marius: It’s do-able, though that’s because I think most things are do-able. The amount of effort is another matter. That’s really delving into multi-blog stuff (even if it isn’t directly multi-blog, it’s dealing with a lot of the same concerns). What it boils down to is: it’s not easy. (Listing the comments in and of themselves wouldn’t actually be that bad; the increased difficulty lies with linking those comments back to the appropriate blog and/or including/using information from the post that contains the comments.)

  21. Scott,

    Have you figured out how to make this code work outside the loop on the main page when the menu code is after the loop? It’s working fine on the individual pages, but it’s not giving the correct info on the index page. Actually, I’d rather it work consistantly outside the loop, since once a person is on the individual page, they have the commenters for that post in front of them already. Is there something I could change in the code to have it work continually outside the loop at all times? (The dynamic part is cool – giving attention to different people on different pages, and I wouldn’t mind having it, but in the meantime…)

    Thank you for a great plugin. πŸ™‚

  22. Scott,

    I don’t know if this would help, but it appears to me that the commenters your plugin is showing on the index page is reading the info from my very first post.

    This post has the ID of 2 (the first being the default wp entry which I have deleted), it still shows up on the index page.

    OOh, ooh! I just figured out what your plugin is reading on the index page! The Oldest Entry currently showing on that page! πŸ™‚ Did I do good? πŸ™‚

    I set the Reading Options in WP Admin to show only the 2 most recent entries, and the data shown in your plugin on the index page coincides perfectly with my oldest showing entry.

    I just tried another setting in the Show the most recent: option setting — and the results are consistant. πŸ™‚

    I betcha you can fix that little anomaly in your plugin now that you know that, huh? :mrgreen:

  23. Valerie: Here’s a quick, untested change you can do to your index.php that should allow the plugin to function outside the loop as advertised. In index.php, look for these lines:

    <?php endforeach; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
    

    And then immediately after that add this line:
    <?php $id = ""; ?>

    I still have to come up with a good way to keep this from being an issue in the next version of the plugin.

  24. Of course, it continues to function outside the loop no matter what page it’s on, now. (I suspect you expected that, but others might not.)

    Anyway, as I said above, I’d rather have top commenters site-wide, given the option, so I’m still glad for the fix. πŸ™‚

  25. I’ve just installed this on one of my weblogs, using the code in a separate file and “including” it in my sidebar, and it then picks up comments from the whole blog. You may need to put the wp header info at the top of that file and include the file as a url …

    It’s a good plugin, and I did want to use the feature to “open in a new window”, but it echo’s target="_blank" when there is no URL identified with the commenters ID (and most of my past commenters have not left a url). No big deal though, I just changed the “true” back to “false” and it displays right, just can’t “force” a “new window”.

    I also have put the in code for on single pages to show that particular post’s top commenters.

    I’m using these features as a “vanity draw” πŸ˜‰

  26. great hack but i do have a problem. how do i set the code so it lists the top commenters like for example

    name (1)
    name (1)

    no matter how much i fix it it either ends up like
    name(1) name(1) or the the names without the (1)

  27. Plugin updated to v1.0

    Changes:

    Verified to work for WP 1.5 (still also works for WP 1.2)
    Fixed bug where “target=” would appear for commenters who did not have URLs
    Renamed function from get_commenters() to c2c_get_commenters()
    Moved WP 1.2 compatibility check outside of the primary function.
    Updated license and examples

  28. Hey Scott, great little plugin. I have one question/request if I may?

    For the Top commenters fucntion, could you have it exclude the blog author from being listed? I of course am the top commenter on my blog and it just seems kinda silly to have my name listed. You know what I mean.

  29. Hi Scott,
    I just wanted to say THANKS! for the plugin; it worked perfectly–no errors, no tweaking, directions were right on the money. (I’m using WP 1.5) For once, a plugin that actually wasn’t a hassle!

    THANK YOU! πŸ˜€

  30. Pingback: ZackScott.net
  31. hi scott. it s wonderfull plugin. but i have got a problem. i placed the code anywhere in my sidebar.php. but it doesn’t show commenter’s all comments’ numbers. i m sure every comments have got e-mails. thanks or the help!

  32. I’ve installed it over WP 1.5 and only shows some commenters at the side bar. With top and recent tags. No $num_people=X control. I’ve installed 1.0 last version.

    Thanks in advance.

  33. I’m using the plugin but the comment count after the commenter’s name appears on a new line. I copied your example code exactly as it is yet it is not appearing as the output.

  34. Pingback: Β«leftbraned
  35. Mine lists two people, both with (1) and they have more. There are other commenters. And it’s listing ME, and I excluded my e-mail (and it’s using e-mails).

    How can we use this in the sidebar?

Comments are closed.