Without a Plugin: Display Recent Comments

This post was originally published on April 9, 2009 and is now being being republished as it has been entirely revised. The original method is removed and now replaced by a new one.

WordPress ships with a Recent Comments widget, which is very handy, but on the other hand does not give you much freedom in terms of look and feel. There are plugins out there with will offer some more functionallity, but you sometimes feel you just want to free yourself form any limitations. So here I present the recent comments function. Let’s get our hands dirty.

Here is the function that you need to put in your functions.php file.

The function has three arguments that allow you to specify the number of comments that you want to show, defaulting to 5; how long you want the comment body text to be, defaulting in 120 characters; and how big you want your gravatar to be, from 0 to 100, defaulting to 48.

Next you want to call the function from the sidebar.php of your theme or wherever else you want them to show on your site. You can do it like this:

And here I’m offering you some boilerplate CSS styles to make your recent comments look good.

I wish you find this article helpful and please make sure to subscribe as more stuff is coming in the Without a Plugin series.

0 responses to “Without a Plugin: Display Recent Comments”

  1. TJ Avatar
    TJ

    This is amazing thank you… it totally worked! and is kickass! Thanks 🙂

  2. BANAGO Avatar

    You’re welcome! I am happy it work for you. Thanks for letting me know!

  3. Andre Avatar
    Andre

    A fantastic post! Interesting read. Going to follow this blog closely in the future. Looking forward to the next update!

    Regards,
    Andre

  4. BANAGO Avatar

    @Andre: Thanks for the good words! You’re most welcome!

  5. kramero Avatar

    Great tutorial! And very useful. I’ve been looking everywhere for this.

    Thanks a bunch 🙂

  6. Pavl Williams Avatar

    Thanks Banago, this is exactly what I was looking for.

    I’m surprised this functionality hasn’t been included in wordpress yet – perhaps you should get onto them?

    All the best and keep up the great work.

  7. Baki Goxhaj Avatar

    Hey Pavl, thanks very much for your nice words.

  8. WpDite Avatar

    Wow man, thats gr8. I personally suggest all peoples not using too much plugins and take the taste of this kinda WordPress code snippets.

  9. Kurt Avatar
    Kurt

    Hi, that’s what I was looking for!

    Is it possible to show comments from a specific category only?

    Thanks!

    1. Baki Goxhaj Avatar
      Baki Goxhaj

      Sorry, Kurt, that’s not possible right now.

  10. natalia Avatar

    Just what I was looking for, thanks for posting this useful code, sorry to bother, but my PHP skills are not very good, is it possible to echo the date of the comment?

    1. Baki Goxhaj Avatar
      Baki Goxhaj

      Yes, it is but you have to edit the comment to achieve that. If other people will be instrested in that, I can add it.

  11. Philip Downer Avatar

    Thanks for the tutorial and code snippet! Looked everywhere for this and finally came across your site… Bookmarked!

  12. Hillary Avatar

    Looking to customize this in a few ways. Firstly, would love to only show recent comments from everyone BUT the blog author. Secondly, is there are way to show the date of the comment? What about the title of the post the comment was on?

    Thanks in advance for any suggestions you can offer!

    1. Baki Goxhaj Avatar
      Baki Goxhaj

      All of those are possible hillary but you have to edit the comment. Perhaps I’ll add the date as an option by default.

  13. […] How to Display Recent Comments Without Using a Plugin or Widget – An excellent tutorial for adding recent comments to your theme using the functions.php file and some code right within your theme files. […]

  14. […] How to Display Recent Comments Without Using a Plugin or Widget » WordPress Freelancer […]

  15. gilkudik Avatar
    gilkudik

    How can I put the post title rather than comment’s text?

  16. chinkone Avatar

    PERFECT! Works like a charm.

  17. Jauhari Avatar

    I was test it and work without any worries 😉

  18. Jen Avatar
    Jen

    There’s no functions.php in my WordPress theme files. Do you know what I should do? I really want this to work. 🙁

    1. BANAGO Avatar

      It’s easy Jen, just create a file and name it functions.php and then paste the code I have provided as is.

      However, the fact that you have not function.php file means that your theme is not widgetized too. You might want to add some widget support to it.

  19. univocus Avatar
    univocus

    How do I add the date/time and category to comment?

  20. Agustín Avatar

    Muchas gracias por esa función! me sirvió un montón! gracias!

    ——————————————————-

    thanks a lot for that function! it really helped me! thanks!

  21. Bev Avatar

    Thanks for the function. Like others, I wanted to display a date with each comment. So I modified the code slightly.

  22. […] Cómo mostrar Comentarios recientes sin utilizar un plugin o widget – Un excelente tutorial para agregar comentarios recientes de su tema usando el archivo functions.php y de algunas de código dentro de sus archivos de tema. […]

  23. gabrielle Avatar
    gabrielle

    Hi there,

    I tried to implemented the code but I have the next error message:

    Fatal error: Call to undefined function bg_recent_comments() in /home/…..

    I appreciate if somebody could help me with this.

    Cheers!!!

    1. BANAGO Avatar

      Gabrielle, it seem like you did not get the main code to functions.php properly. Can you double-check it and let me know?

  24. gabrielle Avatar
    gabrielle

    hi banago

    i actually inserted the code on dreamweaver and then uploaded it by ftp

    but i tried directly from my wp dashboard and didnt work either

    both ways i had the error message

  25. BANAGO Avatar

    Basically, what the error tells you is that you did not put the comments function that you are colling where it belongs at functions.php. If you can let me check your site for a moment, I can do that.

  26. gabrielle Avatar
    gabrielle

    Finally I found the problem. The thing is, in the code theres a space between

    “<" and "?php"

    in both codes (to functions and to sidebar)

    great code btw

  27. BANAGO Avatar

    Yes, there is a space there lol – and thanks!

  28. George Serradinho Avatar

    Hi,

    this is neat and I’m testing it, but i don’t want the admin comments to show. I changed the query a bit to exclude post_author != 1, but the admin comments still show.

    Can you maybe explain to me what needs to be changed/done.

  29. George Serradinho Avatar

    Hi again,

    I found out I needed to use user_id != ‘1’ and now it works.

  30. […] donde el número entre paréntesis es el número de comentarios que queremos que aparezcan. Fuente: https://wplancer.com/how-to-display-recent-comments-without-using-a-plugin-or-widget/ Clic para ver Texto Plano […]

  31. Emmy Avatar

    Thank you, this helped me.

  32. mandekira Avatar

    Hi! Please I’ll like you to help me on how I can display most recent comment of my website on my homepage or on other pages of my website. I uses PHP AND MYSQL for the comment db. Please help me, I’m new to php and dbs in general. Thank you.

  33. Advenda Avatar

    I was test it and work without any worries

  34. Hayley Avatar
    Hayley

    how can i show 10 recent comments?
    copied your codes, but mine is showing only 6..

    appreciate your help 🙂

    thanks.

  35. BANAGO Avatar

    Sure thing, here is the trick. The code I provided uses this code to call the comments:

    Do you see the “6” there? Just replace it with your number, in this case 10. Good luck!

  36. Cippo Avatar

    If I use it in the loop it says ” cannot redeclare … ”

    How can I fix this?

    Cheers,
    Ciprian

    1. BANAGO Avatar

      Try to place the function caller blow the loop.

  37. Julian Avatar
    Julian

    Thanks for a great script! Please can someone tell me how to get and display the comment’s date as well. I have been trying to use get_comment_date and then echo comment_date but they don’t show anything. Thanks.

  38. Dan Avatar

    It was necessary to change the script a little but we managed to make it work properly. I use it on my blog, http://dan-blog.ro you can see this script on my index page, in bottom of the website… Thanks for the info, I have been helpful.

  39. Azah Avatar

    how do I display the comments without admin’s comment. I want to hide my own comment and just display other’s comments.

  40. Ajay Avatar

    Excellent.

    Is it possible to enable paging, seeing as the number of comments is set to 10. it would be nice to scroll through previous comments too.

    Thanks!

  41. Mishelli Avatar

    Is there any specific advantage of using the code instead of the plugin that allows showing recent comments on your blog?

    1. BANAGO Avatar

      It’s just more customizable, that’s all.

  42. daniel Avatar
    daniel

    hi, great piece of code! I have a question, how can I show the post title for each comment?
    Thanks!
    Dani

  43. Jennifer Avatar

    Cool. I am wondering–what’s the advantage of doing it this way as opposed to just using the widget? Just curious.

    1. BANAGO Avatar

      It’s just more customizable.

      1. Jennifer Avatar

        Gotcha! Thanks!

  44. Fily Avatar
    Fily

    Thanks a lot for the code. It worked great after deleting four or five spaces in the <?php ( < ?php delete this space, this is a no, no) tags.

  45. Shraddhananda Avatar

    Hi Bango,

    Thanks a lot in advance.

    Is there any way where recent comment on each category will show in their respective category page.

    Lets assume there are two categories named Math and Science and we had received comment for Math only. So on Math page the comment will appear and on science no comments will appear.

    Implementing this is showing the latest comment everywhere irrespective of categories.

    Best regards,

  46. […] trying to find to way to do is for our own blog. After some googling we stumbled upon WPlancer and BANAGO’s post that gave us the base for the function. That is the beauty of open-source: Spreading awesomeness! […]

  47. Kika Avatar

    Like so many people here we wanted to show the date and post title in our commentary as well. So we modified the original code a bit and made it work in our blog. The full article how to add the date and post title can be found in our post:
    http://www.kikamaca.com/2011/03/show-recent-comments-without-a-plugin/

    Thanks Banago for this wonderful base:)

  48. Sheffield Website Design Avatar

    Dude, bloody brilliant! Worked a treat! 🙂

    @mgpwr

    1. BANAGO Avatar

      Thanks for stopping by 🙂

  49. Spud Avatar
    Spud

    Thanks! I too had to delete some spaces in front of the ?php’s. I also modified the comment text code slightly to include the link on the text and to give an HTML title with the post title:

    <a href="comment_post_ID ) . ‘#comment-‘ . $comment->comment_ID; ?>”>:
    <a href="comment_post_ID ) . ‘#comment-‘ . $comment->comment_ID; ?>” title=””>comment_content), 0, $comment_len)); ?>

    There’s probably a better way, but I’m feeling pretty proud of myself right about now. I would like to truncate the names of the authors at 10 characters — what is the best way to go about doing this?

    1. BANAGO Avatar

      I would like to truncate the names of the authors at 10 characters — what is the best way to go about doing this?

      That will be answered on my next post, How to shorten anything on WordPress. Stay tuned and thanks for the comment.

  50. Robert H. Avatar
    Robert H.

    Good job! Very useful code.

    Is there a way to customise this so that the comment date and post title can display for each as well?

    1. BANAGO Avatar

      Yes, that can be done too.

  51. Madcore Avatar

    Hi. This script was a lot usefull to me, but I trying yo modify the code to make it show the commented post title instead the comment excerpt, but I can´t make it. I tried in diferent ways, but my php knowledge is very basic. Can you help me to make this modification?

    Thanks a lot for the script and for your time doing it an reading this.

  52. Kerala Houseboats Avatar

    Thank you so much…This helped me a lot…

  53. Ammar Avatar

    hi, It’s really great job! it’s so useful, THANKS

    i just wanna ask you how to avoid ping from the comment list?

    i’ll be greatful if you can provide me with a solution.

  54. Ammar Essa Avatar
    Ammar Essa

    Hi BANAGO, i had comment you about how to avoid pings from appearing as a comment in the list, but it’s look like my comment had been deleted!

    Anyway, i’m still stuck on that and need your help to do it!?

    thanks

  55. […] searching about recent comments and AJAX in wordpress i found these nice examples about recent comments in wordpress without plugin or widget and ajax on wordpress themes and thought about implementing real-time recent comments usign […]

  56. AY Avatar

    Thank you a lot, that helped me to reduce the number of sql queries by 4. indeed there is an extra space in the code that gave me a little bit of a hassle.

    thanks!

  57. BrianMI6 Avatar
    BrianMI6

    As others have stated, can I tweak this code so that it only pulls in comments from posts in a specific category? That would be a MAJOR help.

  58. forhad Avatar

    I am new to wordpress. Just trying to show most recent posts in one widget area. And this is what exactly i am looking for. This article also gives idea on custom query in wordpress. Nice reading ! Bookmarked the blog

  59. Baki Goxhaj Avatar
    Baki Goxhaj

    Yep, this is a comment that is shown through the brand new recent comments function. Isn’t that cool 🙂

  60. Khawar Junjoi Avatar

    It’s not working when want to display on specific category.

    1. Baki Goxhaj Avatar
      Baki Goxhaj

      Khawar, please try the updated code.

  61. mn9or Avatar

    Work like magic !

    Thanks for sharing that 🙂

  62. Dhruv Avatar
    Dhruv

    Your method seems to be good but I don’t understand one thing. Why one need to mess up with the coding when already plugins are available. What’s the benefit of messing with coding over using the plugins. Thanks.

    1. Baki Goxhaj Avatar
      Baki Goxhaj

      Some websites won’t be happy with what plugins have to offer – some people want more control.

  63. Roland Avatar

    Thank for your help it works fantastic, but there is a little problem linking to the entire comment, when i click the the name link tag it doesn’t work.
    please any suggestion let us know,

    Thank in advanced.
    Roland

    1. Baki Goxhaj Avatar
      Baki Goxhaj

      You will have to just adjust the code so that you can wrap the parts you want in a link. Take how I produce the link there as an example.

  64. Naomi Avatar

    Nice!! Just a few questions, how can I adjust the comment text that it won’t break off in the middle? That it displays full words and add … afterwards? And how can I add a read on link after the comment instead of displaying the author? Is that possible?

    1. Baki Goxhaj Avatar

      Naomi, it is very possible, but it’s outside the scope of the article. I could help you with your changes in private if you would like.

  65. thuan Avatar
    thuan

    Can you help me add date after comment text ? thank you very much.

  66. ramlan Avatar

    Thank you my friend. Thats code help me, fully. :p

    1. Baki Goxhaj Avatar

      I’m happy to hear that – thanks. 🙂

  67. Jonathan Avatar

    Hey Everyone,

    Saw a question about showing the post title on here. It’s pretty simple. Add this code wherever you want in the list of “$comm”s in the original code.

    $comm .= ‘Post: ‘ . get_the_title( $comment->comment_post_ID ) . ”;

    Hope this helps!

    1. Baki Goxhaj Avatar

      Hey Jonathan, thanks for providing this.

  68. Acan Avatar

    Hi Baki, i think you forget this line ‘status’ => ‘approve’, . Without this, all comments displayed before through moderation. Anyway thanks again..

  69. Dante Avatar
    Dante

    Hey! Thank you veeeery much, I hate to use plugin for small things on wordpress. Sure I’ll subscribe to your blog.

    1. Baki Goxhaj Avatar

      I share the same feeling. Thanks very much.

  70. Hoc Seo Avatar

    Thank you so much!
    But i can’t show comment date with my code:

    get_comment_date( $comment->comment_ID);

  71. Bien Thuy Avatar

    Can we add it as dynamic sidebar?

  72. Mani Fred Avatar

    Hi,

    ty for the great code! But I have a general problem. Every time I put some code in the function.php it works on the frontend but backend isn’t available anymore… only a blank pages appears. The only feature I use in function.php is

    After this I paste your code. Works on frontend, but backend is gone …. would be great if you can help me 🙂

    1. Baki Goxhaj Avatar

      Hey Mani,

      I cannot help of you don’t tell me what actual error you get. You have to turn WP_DEBUG to true in your config.php and see what you get. Thanks for stopping by.

  73. Touraj Avatar

    very very thanks

  74. Johan Avatar
    Johan

    Hi! First of all thank you for the great website!!!

    I would like to extend my current default “Recent Comment Widget” with a date and time. Is that possible?

    1. Jiyong Avatar

      Very nice “plugin” 🙂
      and ty Jonathan for title.

      for date add under “$comm =[…]” :

      ” $d = “l, F jS, Y”;”

      and in loop :

      $comm .= get_comment_date( $d, $comment->comment_ID);

      https://codex.wordpress.org/Function_Reference/get_comment_date
      Hope this helps 🙂 !

  75. Livia Ochmann Avatar

    Thank you very much 😀 It’s working and looks fine.

    1. Baki Goxhaj Avatar

      Really happy to hear that. Keep coding! 🙂

  76. Katie Keith Avatar

    Hi, I would suggest just using a plugin for this if you’re not comfortable with PHP. We have just released a free plugin in the WordPress plugin repository called Better Recent Comments. It allows you to display recent comments in any post, page or widget with options to include the avatar, comment excerpt etc. I hope this is helpful for your readers.

  77. Otti Avatar

    Thanks Baki,
    I believe there is an ul missing surrounding the list items. Apart from that it is working nicely, thanks.
    otti

  78. Ahmed Avatar

    I am working on daily plugins but actually its not working so far.

  79. Ed Linac Avatar

    thank you so very much!

  80. bert Avatar
    bert

    how to prevent duplication of recent comments links