The meat of the plugin is get_recently_commented(), which is primarily intended to return the post IDs of the last N most recently commented posts. The user would then make their own database calls to obtain their desired post information (i.e post’s title, post’s URL) and latest-comment data (latest-comment’s date, latest-comment’s author, latest-comment’s excerpt, latest-comment’s URL) and format that data as they see fit.
That said, in the spirit of providing some reasonable default formatting, the function will by default present an unordered list, each element of which is the hyperlinked post title and the date/time of the most recent comment. The hyperlink is to the post’s individual archive page, to the start of the post’s comment section. See the Examples section to see what I mean.
Also see what the accompanying, usable function, print_recently_commented(), produces.
- If no posts have comments, nothing is output
- Can be used inside or outside “the loop”
- Download the file recently-commented.zip and unzip it into your /wp-content/plugins/ directory.
Copy and paste the the code ( recently-commented.phps ) into a file called recently-commented.php, and put that file into your /wp-content/plugins/ directory.
- Customize print_recently_commented.
- Activate the plugin from your WordPress admin ‘Plugins’ page.
- Call print_recently_commented() (or make use of get_recently_commented()), inside or outside “the loop.”
[For the examples below, I’ve defined the $num_posts arg as 2 for all of the examples in order to limit the amount of sample output.]
get_recently_commented($num_posts = 5, $before='<li>', $after='</li>', $show_pass_post = false)
This is the meat of the plugin that makes the necessary database call to retrieve the IDs of the more recently commented posts.:
- $num_posts : The number of recently commented posts to be returned; default is 5
- $before: The text to display before each recently commented post.
- $after: The text/HTML to display after each recently commented post.
- $show_pass_post : Boolean (‘true’ or ‘false’) indicating if passworded posts should be considered; default is ‘false’
print_recently_commented($num_posts = 5)
- $num_posts : The number of recently commented posts to be returned; default is 5
<?php get_recently_commented(2, '', ''); ?>
[Nothing; returns array of post IDs for use by something else.]
<ul> <?php get_recently_commented(2); ?> </ul>
<ul> <li><a title="View all comments for Some Post" href="http://www.yoursite.org/archives/2004/07/02/some-post/#postcomment">Some Post</a><br /> 07/05/2004@07:23PM</li> <li><a title="View all comments for Another Post" href="http://www.yoursite.org/archives/2004/07/04/another-post/#postcomment">Another Post</a><br /> 07/04/2004@09:42AM</li> </ul>
<div class="rcposts"> <?php get_recently_commented(2, '<div class="rcpost">', '</div>'); ?> </div>
<div class="rcposts"> <div class="rcpost"><a title="View all comments for Some Post" href="http://www.yoursite.org/archives/2004/07/02/some-post/#postcomment">Some Post</a><br /> 07/05/2004@07:23PM</div> <div><a title="View all comments for Another Post" href="http://www.yoursite.org/archives/2004/07/04/another-post/#postcomment">Another Post</a><br /> 07/04/2004@09:42AM</div> </div>
<div class='sidebar'><div class='sidebartop'>Recent Comments</div> <?php print_recently_commented(2); ?> </div>
<div class='sidebar'><div class='sidebartop'>Recent Comments</div> <div class="recentlycommented">07/05/2004@07:23PM:<br /> <a title="View all comments for Some Post" href="http://www.yoursite.org/archives/2004/07/02/some-post/#postcomment">Some Post</a><br /> 3 Comments </div> <div class="recentlycommented">07/04/2004@09:42AM:<br /> <a title="View all comments for Another Post" href="http://www.yoursite.org/archives/2004/07/04/another-post/#postcomment">Another Post</a><br /> 4 Comments </div> </div>
- 27 Sep 2004 : Plugin officially deprecated in favor of Customizable Post Listings
- 07 Jul 2004 : v0.92 — Minor tweaks.
- 07 Jul 2004 : v0.91 — Changed the arguments to the get_recently_commented() function
- 06 Jul 2004 : v0.9 — Released to the public
Copyright (c) 2004, Scott Reilly
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of coffee2code.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 replies on “Plugin: Recently Commented Posts”
Filed under: ???? – ?? @ 2:25
?????? ??????????????????????ä??????????????????????? […]
This looks like a great plugin–as does Recent ! I just have a question though–how would I go about getting this output:
last-commented-entry-name-1: commented by: last-person-to-comment-on-this-entry
last-commented-entry-name-2: commented by: last-person-to-comment-on-this-entry
etc. Is there an easy way to do this, using your plugins?
This is great! I did the same thing in my MT blog and you just saved me many hours – if not days – of figuring out how to re-build it in WP.
I will probably have some questions for you shortly…
Icyshard, go here to see the code for the functionthat you’ll need to use. It’s the first function, sample_recently_commented(). You can paste it into the recently-commented.php plugin file if you want, or into the myhacks.php file (you’ll have to search for help on that; it’s late and I haven’t used the myhacks to begin telling you how to). Hope it helps!
Hey that worked PERFECTLY! Thank you SO MUCH! Thank you for taking the time out to write special code for me. Now I can have recent comments function like it did under MT!
Thanks again! 🙂
My thanks to you, Scott. [Nifty URL.]
I’d like to bundle a modified version of your plugin with a theme I’m working on. I looked for your mail address or some other way of contacting you, but I couldn’t find any.
Could you get in touch?
Hey Scott. I love the redesign!
Thanks again for the customized version of this you built for OrangePolitics.org. I have one issue now: Recently Commented is displaying comments that are still pending approval. (I am actually using a system that allows the users to approve their own comments.)
I can see your code already has “WHERE comment_approved = ‘1’” in it, is there a chance this is clashing with the Comment Authorization plug in?
I must be losing it. I just checked and now I can’t replicate the problem I stated above. So sorry to bug you.
Thanks again for your great work!
i love this plugin, thank you.
i have a question about its implementation:
i’ve successfully added this code to my index:
and on the main index, this general call is fine.
however i have many sub sections to my blog. i wondered if there might be a way to categorize this plugin, so that we might call up:
<?php get_recently_commented(5);$cat=X; ?>
calling categories from recent posts in a given category would be an excellent feature.
ultimately i was hoping to find a way to post “recent posts” as well, but i don’t have the savy to even begin to look at that.
either way, thank you for the excellent plug to wp!
O, woe is me… the problem I mentioned above is back. Recently Commented is showing comments that have not been approved yet. 🙁
Try this: Look for this in get_recently_commented():
AND comment_approved = '1'
And change it to this:
AND $tablecomments.comment_approved = '1'
Does that do the trick?
dss: I’ll see about adding category support into the next version. As for “recent posts”, there is either built-in WP support for it and/or a plugin that might help you there. I assume you just want to list the titles and link to the recent post? You might try MtDewVirus‘s plugin pack, which has a Recent Posts function.
ok, i’ll check it out. but i liked yours cause it was so elegantly simple.
Ruby: I think you mistyped the field name:
AND $tablecomments.comment_approved = '1'
is what you want. Looks like you had
AND $tablecomments.comment-approved = '1'
(you had a hypen where you needed an underscore)
dss: Thanks for the compliment. I am thinking of modifying the plugin to be more flexible, in which case I may have it also handle Recent Posts in a similar fashion.
D’oh! For some reason, I can’t select the text on your blog so I had re-typed it – sloppily. Thanks.
So now it’s not breaking, but it’s still doing it. Should I also replace
WHERE comment_approved = '1'?
OK, now it appears to be behaving properly!
I like it when things work, but I wish I understood why… I think there may be some weird cacheing thing hapenning on my server.
Sorry to be such a pain again! Scott your ‘customer service’ is astounding. Do you have a tip jar or wish list?
I’m not sure if it’s related, but just in case it is… around the time that I did this fix for Recently Commented, another plugin broke. I am using Scott Merrill’s excelltn Comment Authorization to make commenters verify their e-mail address. Now when folks click the link to approve their comment, they get the error:
Is it possible these two are interfering?
i dont quite get it. maybe it’s because i’m new to wordpress, but after installing everything, i click on the link to show the post with recent comments. however none of the entry’s content is displaying. how do i set it so that all the stuff in my normal entry is displayed?
i’m also using a popup comments btw. i’m not sure if this would cause problems.
Ruby: Did you get your situation sorted out? I know you were working with Scott Merrill over e-mail to see what was the source of your problems, but you seemed okay as far as this plugin was concerned, am I right?
Tin: When you followed the link to the comments, did you scroll all the way up? The link takes you to the permalink page and scrolls you down to where the comments are. Having popup comments shouldn’t make a difference. If you would rather link to the content of the post (and not down to the comments section for the post), change this line:
echo $before . '<a title="View all comments for ' . strip_tags($title) . '" href="' . get_permalink($post->ID) . '#postcomment">' . $title . '</a><br />' . "\n";
echo $before . '<a title="View all comments for ' . strip_tags($title) . '" href="' . get_permalink($post->ID) . '">' . $title . '</a><br />' . "\n";
Basically you are taking out this:
it still doesnt work. can you visit the site and check it out? thank you so much.
I’ve got a simple issue. I am using this plugin, but altered the echo’d code to get it to display like I would like. One thing though, I would like it to display the comments and repeat certain posts if there are more than one comment. (ie Jim on Post1…Paul on Post2…Bart on Post1) I hope I explained it correctly. Thanks
Nevermind, had a brain-fart should have read the title of the plug-in. I had to get MtDewVirus’ Recent Comments. Sorry.
hi scott. i managed to fix some of the issues. i only have one problem left. after i click on the most recent comment, it takes me to the entry with the most recent comment. that works fine. however the link that normally displays the popup comment link is not there. it’s present anywhere else on the site except with permalinks for some reason. do you know why?
Tin: The permalink page is the archive page of both the post content and the comments for that post. Because the comments are already on that page, there is no need to display the link that does the popup comments. So the behavior you are describing is how things work. You could probably change it if you were inclined.
ooo gotcha. i understand now. thank you for your articulate explanation. i have one more question that is about something else. where can i modify the settings so that instead of displaying the post title in the list of most recently commented, it would display the name of the commenter. or should i be looking at the most recent commenter function?
actually the most recent commenter plugin doesnt display a link to the post.
is there a way to display the name of the commenter as well as linking it to the post?
Tin: I’m currently working on modifications to the plugin that will allow you define precisely what gets displayed and how it gets displayed. I don’t know when it’ll be ready, so check back again later if you’re still interested.
I just started using WP and discovered your site very recently. I was wondering what is the difference between this plugin and another one you wrote called “Customizable Post Listings”, as far as the recently commented posts are concerned? Is “Recently Commented Posts” a subset of the other?
Louis: Hi, and welcome to WordPress! Actually, you’ve brought up something I’ve failed to note in the post for this plugin, which I have just now done: This plugin has been officially deprecated, as the Customizable Post Listings has this exact functionality (and much more) and allows for customization of the output (which this plugin didn’t).
[…] « I Seek You ?????? Recently Commented ???? ????? ?????? Recently Commented Posts (????????? ?????????????? ?????) Scott&# […]
I like your site. My site about satellite dish
Great plug-in! I just found one bug, in the comment count it counts comments that have been caught by my spam program Spam Karma 2. So it lists a number that is much higher than actually exists.
I can only imagine the spma will increase so it is a problem. It even lists posts that actually have no comments because of this problem. Any ideas?
comments_approved=’1′ is how mine already reads.
[…] ???? ????? ?????? Recently Commented Posts (????????? ?????????????? ?????) Scott’a Reilly. ?????? ?? ??????????, ????? ????????? ? ??????????????. […]
Thanks for the great plugin! This is exactly what I need.