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!


Comments

31 responses to “Show recent comments of a particular user in WordPress”

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

    1. Thanks Ravi!
      WordPress is built in PHP & MySQL.

  2. […] This post was mentioned on Twitter by Navin Poeran, Ashish Kumar. Ashish Kumar said: Show recent comments of a particular user in WordPress http://bit.ly/g7m4h4 […]

  3. 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. 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. ๐Ÿ™‚

  4. Elliot Avatar
    Elliot

    Just what I have been looking for.

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

    1. 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 Avatar
        Elliot

        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. I meant the code used for displaying comments on posts in your theme files.

          1. Elliot Avatar
            Elliot

            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. 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 Avatar
                Elliot

                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. Okay! I get it. Add this after line 29:

                'post_id' => 145, // change 145 to what you want
                
  5. Elliot Avatar
    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!

  6. Elliot Avatar
    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. I believe you already have what you need. If you need more customization, you can hire me.

      1. Elliot Avatar
        Elliot

        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.

  7. Elliot Sowersby Avatar
    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?

  8. 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

  9. 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.

  10. 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 ๐Ÿ™‚

  11. 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. There is an unsubscription link in the email itself. Like when you get this reply, find the link in the email at the bottom.

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

    Thank you!

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

  14. 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)

  15. 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…

  16. 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. I had this, just can’t quite get it right to work:

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

  17. Elliot Sowersby Avatar
    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 Avatar
      Elliot Sowersby

      This is the solution for anyone looking:

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