How To / Tutorials 2 min read

Show recent comments of a particular user in WordPress

A friend asked for a help in his project where he required to list all the comments a registered user made on the site. So just for play, I created a small plugin which provides you with a shortcode which you can use on a page link example.com/my-comments/ or whatever.

I am zipping it up as a plugin but you can copy paste the code inside your functions.php file and you should be fine.

<?php
/*
Plugin Name: Show Recent Comments by a particular user
Plugin URI: http://blog.ashfame.com/?p=876
Description: Provides a shortcode which you can use to show recent comments by a particular user
Author: Ashfame
Author URI: http://blog.ashfame.com/
License: GPL
Usage: 
*/

add_shortcode ( 'show_recent_comments', 'show_recent_comments_handler' );

function show_recent_comments_handler( $atts, $content = null )
{
	extract( shortcode_atts( array( 
		"count" => 10,
		"pretty_permalink" => 0
		), $atts ));

	$output = ''; // this holds the output
	
	if ( is_user_logged_in() )
	{
		global $current_user;
		get_currentuserinfo();

		$args = array(
			'user_id' => $current_user->ID,
			'number' => $count, // how many comments to retrieve
			'status' => 'approve'
			);

		$comments = get_comments( $args );
		if ( $comments )
		{
			$output.= "<ul>\n";
			foreach ( $comments as $c )
			{
			$output.= '<li>';
			if ( $pretty_permalink ) // uses a lot more queries (not recommended)
				$output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
			else
				$output.= '<a href="'.get_settings('siteurl').'/?p='.$c->comment_post_ID.'#comment-'.$c->comment_ID.'">';			
			$output.= $c->comment_content;
			$output.= '</a>';
			$output.= "</li>\n";
			}
			$output.= '</ul>';
		}
	}
	else
	{
		$output.= "<h2>You should be logged in to see your comments. Make sense?</h2>";
		$output.= '<h2><a href="'.get_settings('siteurl').'/wp-login.php?redirect_to='.get_permalink().'">Login Now &rarr;</a></h2>';
	}
	return $output;
}
?>

You can use in a post or page as follows:

[show_recent_comments]

This will show last 10 comments of the current user.

[show_recent_comments count=15]

You can change the number of comments you want by providing it with the value of count.

Now, if you notice, they do link to the respective comments but links are not pretty permalinks. I don’t recommend you do this (because it increases the query count), but if you really want to have pretty permalinks, you can use the shortcode as follows:

[show_recent_comments count=15 pretty_permalink=1]

or just with the default count of 10 as follows:

[show_recent_comments pretty_permalink=1]

I should highlight the fact that the current user (registered member) will see the comments he or she has made. If you are not logged in, it asks you to login and after login brings you back on the post or page where you are using this shortcode.

You can use this code to make changes as per your need. You can use this shortcode anywhere in your theme by calling it as follows:

<?php echo do_shortcode( '[show_recent_comments count=15]' ); ?>

And I think if you use it like that outside the loop, it won’t bring the user back to the previous page. You will need to change the get_permalink() call near the end of the plugin.

Good thing is that it works correctly with cache plugins such as WP Super Cache or W3 Total Cache because logged in users are served live pages instead of cached pages. Correct me if I am wrong.

Download Show Recent Comments plugin

Other than that, if you have any questions, comment section is all yours!

31 comments

  1. Ravi Shankar

    Awesome ashish, well which programing language do you use to code WP plugins and how can i learn it.

    1. Ashfame AUTHOR
      replying to Ravi Shankar

      Thanks Ravi!
      WordPress is built in PHP & MySQL.

  2. Ileane

    Hey, this is pretty neat. I have a question for you – how many WordPress plugins have you developed and where can I have a look at all of them? Thanks!

    1. Ashfame AUTHOR
      replying to Ileane

      I was only making paid plugins for clients only so far now. I am a full time freelancer web developer.
      Facebook Thumbnail fix was the first public release. I have plans for coding up some plugins on cool ideas, I would suggest you to subscribe so that you don’t miss any updates.

      Also this page will show all of my plugins hosted on WP repositories – http://wordpress.org/extend/plugins/profile/ashfame

      But sometimes I do things (like on this post), which don’t make it to the public release.

      Let me know if you need help with anything. 🙂

  3. Elliot

    Just what I have been looking for.

    However I wondered is it possible to show the users comments for a certain page?

    1. Ashfame AUTHOR
      replying to Elliot

      Thats how the WordPress works by default. Take in the code for comments from post template file (single.php) and copy over to the page template file (page.php).

      1. Elliot
        replying to Ashfame

        Not sure if I understand, so this would make the code “[show_recent_comments]” on a page display a users comments only for that page?

        1. Ashfame AUTHOR
          replying to Elliot

          I meant the code used for displaying comments on posts in your theme files.

          1. Elliot
            replying to Ashfame

            Still dont understand.. could you possibly give a more detailed explaination?

            What I need is so I can enter a code and it will show the comments by a user for a specific page and not all pages like this does.

            1. Ashfame AUTHOR
              replying to Elliot

              You have comments on your posts, right? You want comments on your pages too, right? So, take the code from single.php of your theme, and put it in page.php

              1. Elliot
                replying to Ashfame

                No, what I mean is I would like to display a list all the comments a registered user made on certain page (for example page id 145) and not for the whole of the site like this plugin does.

              2. Ashfame AUTHOR
                replying to Elliot

                Okay! I get it. Add this after line 29:

                'post_id' => 145, // change 145 to what you want
                
  4. Elliot

    Ok thanks alot!

    Also is it possible to have it so I can display all comments by a users on 1 page, but on another page have what I explained above and only display the comments for a certain page? If so this would be great!

  5. Elliot

    I also would like to be able to display it on different pages and display a list all the comments a registered user made on that page… is this possible?

    1. Ashfame AUTHOR
      replying to Elliot

      I believe you already have what you need. If you need more customization, you can hire me.

      1. Elliot
        replying to Ashfame

        Im just unsure how I can make it so I can put it on different pages/posts a list all the comments a registered user made for that page/post? So I can have more than one page with this feature.

  6. Elliot Sowersby

    Hello,

    When I enter this code: in my template now or on a page it displays the comments all on 1 line and doesnt display breaks.

    So a comment like this:
    “Hello,

    This is a test.

    Kind regards,
    Elliot”

    Would show as:

    “Hello, This is a test. Kind regards, Elliot”

    Any suggestions?

  7. Avinashbhageloe

    Is there a way that i can display content of a page to a user that is logged in using username?

    Example:
    On the same page there are 5 lines of text:
    Line 1 with text
    Line 2 with text
    Line 3 with text
    Line 4 with text
    Line 5 with text

    Line 1 must only display if user 1 is logged in
    Line 2 must only display if user 2 us logged in
    Line 3 must only display if user 3 is logged in
    ect.

    Mvg,
    Avinash Bhageloe

  8. Todd

    Hi great post.

    Just wondering if you have any code for listing the comments of the users whose profile page I’m on. So for instance User A has made 10 comments throughout his experience on our website. User B then goes to User A’s page. I would like to make it so User B who is visiting User A’s page can see all User A’s comments.

    Thanks.

  9. Xaliber

    Awesome one!

    One question, though: is it possible to make it so it lists all comments (not just 10 recent comments) that a user have made since their very first comment? Something like those in traditional forum boards 🙂

  10. Ivan Haecker

    Once I initially commented I clicked the -Notify me when new feedback are added- checkbox and now each time a comment is added I get four emails with the identical comment. Is there any manner you may remove me from that service? Thanks!

    1. Ashfame AUTHOR
      replying to Ivan Haecker

      There is an unsubscription link in the email itself. Like when you get this reply, find the link in the email at the bottom.

  11. Loverock

    Hi, what if a user has no comments to show? how can I sent a message if messages count is 0?

    Thank you!

  12. Chris

    is is possible to show the users comments even if they are not logged in?

  13. Andrea

    Hi Ashish, this plugin is what I was looking for but I need your help to make it perfect for my needs.
    It displays the content of the comment ad the link to the comment in its post.
    I’d like to have shown:

    • Title of the post (link to the post)
    • Comment

    It would make my user profile page so neat!

    Thanks for your reply!!!
    Andrea (Italy)

  14. Andrea

    I would also suggest to create it the same way the “Contributions – Reviews” is made. I could also create by myself but I would need the tags for the post title, date of comment…

  15. Kyle

    Hey Ashfame, great piece of code! I looked high and low for this. I see no one has posted in awhile, but it still works just as good now. I was wondering how I could drop in more meta information on the comment (i.e. page title and date)?

    I just don’t know the output code. My guess was that it would go right above the line ‘$output.= $c->comment_content;’. I couldn’t figure out how to display the title $output.= $c->comment_page_title;’ or something? Do I need to add a foreach line above?

    1. Kyle
      replying to Kyle

      I had this, just can’t quite get it right to work:

      <a href="comment_ID ) ); ?>”>

  16. Elliot Sowersby

    Hello,

    I am looking to get the comments of the logged in user for the page which the shortcode is posted on.

    I have the following so far which gets all the comments by that user on a specific page ID, but I would like it so that it gets whatever page the code is posted on and shows the comments for that page.

    ‘post_id’ => 145, // 145 is the page id

    1. Elliot Sowersby
      replying to Elliot Sowersby

      This is the solution for anyone looking:

      ‘post_id’ => $post_id = $GLOBALS[‘post’]->ID, // ID is what post wanted

Comments are closed.