A

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 […]

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 responses to “Show recent comments of a particular user in WordPress”

  1. Ravi Shankar says:

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

  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. Ileane says:

    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!

    • Ashfame says:

      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 says:

    Just what I have been looking for.

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

    • Ashfame says:

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

      • Elliot says:

        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?

        • Ashfame says:

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

          • Elliot says:

            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.

            • Ashfame says:

              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

              • Elliot says:

                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.

              • Ashfame says:

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

                'post_id' => 145, // change 145 to what you want
                
  5. Elliot says:

    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 says:

    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?

    • Ashfame says:

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

      • Elliot says:

        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 says:

    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. Todd says:

    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. Xaliber says:

    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. Ivan Haecker says:

    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!

  12. Loverock says:

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

    Thank you!

  13. Chris says:

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

  14. Andrea says:

    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. Andrea says:

    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. Kyle says:

    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?

  17. Elliot Sowersby says:

    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

    • Elliot Sowersby says:

      This is the solution for anyone looking:

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