0

I've got an if ($comment->user_id) block to pick an default admin comment gravatar (different than a non-user's). But I'd still like this gravatar to be used if a user comments using their registered email, even if they comment while not logged in. What is the best way to test for this?

EDIT

I'm already using

 if ($comment->user_id){
            //comment by registered user
            $avatar = '/images/bird_comments_big.png';
            }else{
            //comment by none registered user
            $avatar = '/images/bird_comments_pink.png';
            }

with

<div><?php echo get_avatar($comment, 70, get_bloginfo('template_url').$avatar); ?></div>

If the user is logged in a makes a comments, the if statement is indeed fulfilled. However, if the user uses their registered email (i.e. tied to their login in WP) without bring logged in, user_id is not present, and the icon for the unregistered user appears. user_id is only stored if the user is logged in.

I also want to test if the email given matches a registered user, regardless if they are logged-in or not.

3
  • You can use get_ avatar codex.wordpress.org/Function_Reference/get_avatar
    – Bainternet
    Apr 8, 2011 at 3:09
  • @Bainternet Yes, I do eventually use this function, but I'm trying to have a default user avatar that is different than the default commenter avatar. So I need a way to determine which default to pass to get_avatar(). Apr 8, 2011 at 12:32
  • get_avatar() accepts email so you can pass the comment email
    – Bainternet
    Apr 8, 2011 at 12:42

2 Answers 2

3

You can simplly check

if ($comment->user_id > 0){
//comment by registered user
}else{
//comment by none registered user
}
3
  • I don't think you're understanding.... I already posited this part in my question. I'm going to try and rephrase... Apr 8, 2011 at 13:24
  • 1
    @two7s_clash When a non user comments, his $comment->user_id values is set to 0 So @Bainternet provided simple way to distinguish registered user's comments.
    – amit
    Aug 21, 2012 at 21:28
  • I totally get that and I was already doing a version on that in my example. Sep 4, 2012 at 22:28
0
if ($comment->user_id || email_exists($comment->comment_author_email)){
            //comment by registered user
            $avatar = '/images/registered_user.png';
            }else{
            //comment by none registered user
            $avatar = '/images/non_registered_user.png';
            }

with

<div><?php echo get_avatar($comment, 70, get_bloginfo('template_url').$avatar); ?></div>

Seems about as succinct a way I can figure.

2
  • I guess, the $comment->user_id is a string. ('0' / '1'), So the way you're using it in if statement is wrong.
    – amit
    Aug 21, 2012 at 21:25
  • It does what I want it to do. You find this semantically wrong? Sep 4, 2012 at 22:27

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.