1

I want to display the commenters username or login name in a comment template. I know there is a function comment_author_link(), but this displays the "display name publicly as" name and not necessarily the username. Usernames have to be unique so I would prefer to display the username. I have tried some other functions and have been looking all over for this. The closest I got was the following function:

<?php
if ($comment->user_id) {
    $user=get_userdata($comment->user_id);
    echo $user->user_login;
}
else {
    comment_author_link();
}
?>

But for some reason this only displayed the name for the first commenter and then it does not display a name.

How do I display the commenters username or login name?

Thank you.

  • 3
    Unicity is not really a problem in your case, but I don't feel it's right to expose usernames...For the question, please put some more code. – Nabil Kadimi May 30 '14 at 22:35
  • The code in question works. Point (and somewhat a problem therefore) is, that the login name as well as the password should not be shared to the public. While it's ok for me to give you the username for my login (I know that my password is hard to crack and that I lock down too many attempts properly), I am not too sure if you would like script kiddies trying to steal your system users identities. In other words: There is a reason why it is named user_login and user_displayname. :) – kaiser May 31 '14 at 0:49
0

If the person commenting is already logged in you could capture the user object like so $user = wp_get_current_user(); and then simply grab the username like $user->user_login. Once you have this it shouldn't be too hard to add in to the creation of a comment so its only asking for the username alone and only once logged in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.