WordPress plugins, themes, tips and hacks

Recent Comments code snippet

November 9, 2007 – 7:19 am | by Miriam Schwab

This code is from the Blog Oh Blog theme, and displays recent comments in the sidebar without the need for a plugin:

<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "\n<ul>";
foreach ($comments as $comment) {
$output .= "\n<li>".strip_tags($comment->comment_author)
.":" . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\" title=\"on " .
$comment->post_title . "\">" . strip_tags($comment->com_excerpt)
."</a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;
echo $output;?>

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Related posts

  1. 24 Responses to “Recent Comments code snippet”

  2. By Hannit on Nov 9, 2007 | Reply

    ?ust to mention recent comments is a built-in plugin, so there is no added value in putting it as a standalone code.

    The advantage of built-in plugins is that they are updated as part of the main WordPress version and will always work with the installed version.

    I see no real reason for using a code instead of plugin in this place since it will not even improve theme performance and if the theme does not support widgets - well … widgetize it, that’s much more simple and has more benefits.

  3. By Miriam on Nov 10, 2007 | Reply

    Hannit - what do you mean by a built-in plugin? If you mean that there is widget that does this for you, I’ll tell you why I find this type of code useful:
    1. I don’t use widgets. I find them to generally be too inflexible for my needs.
    2. There is a plugin for Recent Comments for the others like me who don’t use widgets, but now I prefer to replace plugins with code in the template files wherever possible. The reason for this is twofold: a) I prefer to keep the number of plugins that I use as low as possible; b) I recently worked with a corporate client who could not give me access to their server. Something happened when their guy installed WordPress, and any plugins in folders were not recognized by WordPress! This is just one example of where it’s best to work with code instead of plugins.

  4. By Hannit on Nov 10, 2007 | Reply

    as for 2) I’d rather help them fix the problem. this might affect other aspects of WordPress.

    Why do you prefer to use minimum plugins ?
    I’d rather give people maximum flexibility and leave the power in their hands.
    There are lots of recent comments widgets/plugins - some are more flexible then other.

    I’d really like to know why don’t you use widgets. widgets give you high flexibility with functionality position.

  5. By Ryan on Nov 10, 2007 | Reply

    Why would anyone want to use widgets? I haven’t found a use for them yet. Apart from maybe giving someone else the ability to move things in and out of their sidebar. If you are the only one needing to edit your sidebar, then it makes sense just to edit the code directly IMO. Or maybe there’s something that widgets can do that I am unaware of?

  6. By Hannit on Nov 11, 2007 | Reply

    Rayn - this is the main reason to use widgets, most of the WordPress sites I set up are not for myself, and I want to give the owners the ability to play around with their sidebars content.

    Once widgets were integrated with WordPress core, I see no reason NOT to use them, would love to hear yours.

    IMHO widgets are good for you, esp. if you’re not a coder, but even if you are. There is no need to re-write a code that has already been written and is maintained regulary.

    I can write whatever plugin/widget I want, but then again I can write my own FS or OS and I don’t do that…
    Someone wrote it? It’s working ? why not use that instead of writing you’re own which takes more time and might contain bugs.

    As to putting code in themes, I do that on my own theme, but I really try to avoid it on clients themes. Putting a custom made code INSIDE the theme and not in a widget requires the client to contact you for every change. I’m all forward to giving my clients a free hand and make them come back to me because the want, not because they have to.

    Just my 2 cents

  7. By Ryan on Nov 12, 2007 | Reply

    I’m not convinced that using a widget is any faster than hard coding most things though.

  8. By Hannit on Nov 12, 2007 | Reply

    faster in what way? performance wise - of course it isn’t. but that’s not the point (not mine anyway)…

    It is faster for use, and easier for the client to use.

  9. By Ryan on Nov 12, 2007 | Reply

    I meant I can hard code it faster than I can install a plugin. To use a plugin I need to find the one I want, download it, upload it, activate it, stick it in the sidebar with the widget thing and hope that it works.

    Plus you need to deactivate them all every time you want to upgrade.

    I’ll have a fiddle with the widget system now though, I haven’t tried it since I first started out with WordPress as I couldn’t see any use for it.

  10. By Ryan on Nov 12, 2007 | Reply

    I had a crack with the widgets a few minutes ago in my blog (http://ryanhellyer.net/). But it messed everything up in my sidebar.

    1. The “Search” title was missing from on top of my search bar.

    2. When I tried to add pages, it just added an extra bunch of pages below my regular ones.

    3. My categories displayed some which I didn’t want displayed there.

    4. My archives list showed every month rather than years.

    Are these things doable with widgets? Adjusting all these settings directly in the code is easy, only takes a few seconds to change them. But I can’t figure out how to do it using the widgets - although I didn’t try very hard.

  11. By Hannit on Nov 12, 2007 | Reply

    Ryan, I’m not trying to preach for widgets. In you’re blog you should do what’s good for you.

    My main problem is with using these things in clients installation.

    As for being able to write code more easily then installing a plugin - well, that depends on the specific plugin :-)

    All the things you mentioned are doable with the right widgets.
    Maybe not the default ones, but certainly doable :-)

    As said before, I’m not trying to “convert” you, since as a ‘coder’ you can handle you’re own stuff nicely, I think that supplying your clients with a theme that will give them the option to fiddle around - is good business practice, that’s all.

  12. By Sharon on Nov 12, 2007 | Reply

    Ryan - I think you’re slightly confusing between yourself as a poweruser and the Average Joe who’s clueless as to where the hell stuff goes in PHP code and whatnot.

    They always say - “Its easy when you know how”. And they’re right. For you, hardcoding custom stuff is faster than activating a plugin, but for most users, widgets are the way to go, since they make life easier.

    The way I see it, widgets bring us one step closer to making WordPress a better, more extendable platform that anyone can use, even non-techie folks who don’t speak PHP.

  13. By Ryan on Nov 12, 2007 | Reply

    Yeah, I agree that some plugins are definitely best left as plugins, I was more referring to basic theme stuff like Miriam has been posting lately. Some I wouldn’t have the foggiest idea how to make! - complete lack of programming knowledge :P

    From what I can tell, you can edit the widgets code from the wp-includes/widgets.php file. I’ll keep that in mind in case I need to give someone else the ability to shunt stuff around on their page. Looks like another toy to play with :)

    Hannit, do you have a link to an ‘idiots guide’ to widget development? If I’m bored some day I might have a try at making some. Could be a useful addition for designing CMS’s.

  14. By Hannit on Nov 12, 2007 | Reply

    Ryan, I assume you do not read Hebrew, so I’ll not suggest to write one (though I should). I’ll make sure to look for one and send you :-)

  15. By Miriam on Nov 12, 2007 | Reply

    Hi everyone - glad to see that such a lively conversation is taking place here!

    Hannit, you make some very good arguments, and I’m so convinced that I’m going to try widgets again on my next blog installation. I agree with you about client usability, and like you I try to make sure that the blogs I build are really easy for the user so that they don’t have to come back to me for every little thing. But the truth is that most of them don’t want to touch their sidebars - even widgets are too complicated for some of them.

    I remembered why I haven’t been using widgets. There were a few blogs that I tried them out on, but then I wanted or needed to add something kind of “fancy” to the blog, and I couldn’t do it with the widget. This happened a few times, until I gave up.

    I’ll let you all know how it goes with my next venture into the world of widgets!

  16. By Ryan on Nov 12, 2007 | Reply

    Lol, I am certainly no poweruser or expert at PHP :P But thanks for the compliment :)

    My knowledge of programming is almost zero! I just hack at these things and google for bits of code that I can’t hack into place :P

    I do entirely agree though that widgets have their place. I was just saying that I didn’t see any use in using them myself. I don’t design blogs for anyone but myself and have never had a client, so the uses are negligible. Having said that, I am warming to the concept of setting up a CMS system which uses widgets, as it would be nice to give other users of one of my sites the ability to shunt stuff around various pages.

  17. By Hannit on Nov 12, 2007 | Reply

    Ryan, Miriam - since version 2.2 widgets are part of WordPress core, even for you’re own benefits write widgets that contain the needed code.

    This will give the clients and you the ability to control things using wordpress backend.

  18. By F-dub on Dec 10, 2007 | Reply

    This works great! Thanks, I looked all over, and most of the code snibbits didn’t work. This one is perfect -

  19. By Miriam on Dec 10, 2007 | Reply

    Hannit and Ryan - I just widgetized one of our projects, and I have to say that I am pretty much converted now! There were some glitches, but there are ways to overcome them with plugins. For example, I found the tag cloud widget to be pretty annoying since it wasn’t in a div so it spilled outside of the margins. But I found a plugin that gives you a more customizable way to create a tag cloud, and it puts the whole thing in a div, which I could style.

    Hannit, as you said - it’s a great way to allow clients to have more control over their sidebars. Why shouldn’t they be able to easily modify what they have there?

  20. By Christina on Mar 17, 2008 | Reply

    Widget or no widgets, I think I’m right in saying that this code can be used anywhere?!

    I’ve always found the footer to be the best place for recent comments and recent posts. Far better than a sidebar!!

  1. 5 Trackback(s)

  2. Nov 11, 2007: ?????’???, ?? ?? ?? ?-- ????? ?? ??"? ???
  3. Nov 20, 2007: Code: Adding Recent Comments To Your WordPress Theme
  4. Mar 17, 2008: Mastering Your WordPress Theme Hacks and Techniques
  5. May 6, 2008: SEO & Web Design » Blog Archive » Mastering Your WordPress Theme Hacks and Techniques
  6. May 6, 2008: SEO & Web Design » Blog Archive » Mastering Your WordPress Theme Hacks and Techniques

Post a Comment

Revolution Premium Themes