views:

168

answers:

3

I have a Wordpress blog set up to display comments as "Anonymous User" by hard coding it into the comments.php file. I would like to have it say the user's Username next to their comment and ONLY display that Username to THEM. In other words, if they're a guest, they'll see "Anonymous User" and if they're a registered/logged in DIFFERENT user, they'll still see "Anonymous User", but if it's THEIR comment it'll say "Your Comment" or their own username. Any clue on a snippet of code? Here's what I have so far:

Anonymous User: <div class="post-txt" id="<?php comment_ID() ?>"><?php comment_text() ?></div>

Thanks!

+2  A: 
function my_custom_comment_author_filter($author){
  global $current_user;
  wp_get_current_user();
  if(!is_category(3)){
    return $author;
  }
  if(0 == $current_user->ID || ($current_user->display_name !== $author && $current_user->user_login !== $author)){
    return 'Anonymous User';
  }
  return $author;
}

add_filter('get_comment_author', 'my_custom_comment_author_filter');
John P Bloch
you'll need a 'varies: Cookie' header if you allow any caching.
symcbean
@symcbean true.Also, if you want the comments to show the post author's name to everybody, you'll need to add another check against the author. Also, it might be a good idea to use only the comment author's name, not the comment author's link.
John P Bloch
Ok so I tried this way, and at first I was getting an error so I had to remove the = sign between the first two words (function = my_custom...) and then it threw another error, unexpected $end, so I added a second close } after the last one and before the add_filter. Now, no more errors, but, when I try to put my new function in my theme file, I get another error, telling me that it requires args. I'm not sure what args to use. I was typing "<?php my_custom_comment_author_filter() ?>" but apparently that wasn't working. Any ideas? thanks much!
RodeoRamsey
yes. You need to keep the $author in the function declaration. That's how the argument is passed into the function. I'll edit the answer to reflect your changes. Sorry about those. I guess I just missed them...
John P Bloch
Ok well for some weird reason adding this to my functions.php file and not doing anything else applies this to the Admin dashboard (comments section). Any idea why? I only want this function to work for the front end. I want anybody who has access to the back end be able to see the correct usernames.
RodeoRamsey
Also, what if I want this to be category specific? So let's say Cat-1 has a specific comment form and Cat-3 has a different one. How do I use this only for Cat-1?
RodeoRamsey
@RodeoRamsey that probably has something to do with your comments theme file. If it's not using the comment_author() function, this filter wouldn't be triggered. I've added a check for categories. If you only want it to work on a specific category, put the ID, name, or slug of the category where the number 3 is. You can also check for multiple categories by using an array as the argument.
John P Bloch
+1  A: 

Check if the current visitor is logged in http://codex.wordpress.org/Function_Reference/is_user_logged_in

<?php if ( is_user_logged_in() ) { 
    ....
} else {
    ....
} ?> 
JeremySpouken
Well, that only works to let me know if I'm logged in or not but it doesn't separate the two conditional statements.I think that's the right start, but I'm thinking it needs to say something like "if user is logged in, AND, user name is = to same login" but I have no idea how to code that...
RodeoRamsey
+2  A: 

Basically, you will need to get the comment author's ID, get the logged in user's ID and compare the two. Have a look at getting the current logged in user and getting information about the current comment from the Codex.

I haven't tested this snippet, but it should point you in the right direction:

<?php global $user_id, $user_login; 
    get_currentuserinfo();  // This will populate $user_id with the logged in user's ID or '' if not logged in
    $the_comment = get_comment(comment_ID());  // Get a comment Object...
    $author_id = $the_comment->user_id; // and extract the commenter's ID

    if($user_id !== '' && $author_id == $user_id){
        echo 'Your comment [ ' . $user_login . ' ]:';
    }
    else{
        echo 'Anonymous User:';
    }
?>
ajm
John P. Bloch's answer above is really nice. Adding that to your functions.php file would allow you to call that anywhere.
ajm
I tried the above answer and couldn't get it working so I tried your answer to see if it would work and it seems to add a digit next to the Anonymous User text but it doesn't display correctly. Thanks!
RodeoRamsey
Could you try echoing out $user_id, $author_id and $user_login to see what comes back? Are you seeing the current logged in user's details and the comment author's ID?
ajm