1

I am trying to display user's avatar/profile image when logged in, but when I use this:

 <?php
    global $current_user;
    get_currentuserinfo();     
    echo get_avatar( $current_user->ID, 64 );

?>

it displays the default_avatar_male.jpg, but I don't want anything to be displayed unless logged in. Thanks for all help.

5

Hey pass the current user email id in get_avatar() function if user is logged in like this

<?php 
if ( is_user_logged_in() ) {
    $current_user = wp_get_current_user();
    if ( ($current_user instanceof WP_User) ) {
        echo 'Welcome : ' . esc_html( $current_user->display_name );
        echo get_avatar( $current_user->ID, 32 );
    }
}
  • I tried your code as well and it still worked just as well as Rohil_PHPBeiginner's above. My additional question which I posted above is how would I be able to display it like: Welcome Tom "profile image" ? – Dave_O Dec 31 '14 at 17:42
  • Hey Please check the above edited answer – Amit Mishra Jan 1 '15 at 6:38
0

You can wrap this code in to a condition that if user is logged in then show profile pic and for that you can use wordpress function is_user_logged_in.

So your code will be something like this :

<?php
    global $current_user;
    if ( is_user_logged_in() ):
        get_currentuserinfo();     
        echo get_avatar( $current_user->ID, 64 );
    endif;  
?>
  • Great! this worked well. How would I be able to display user's first name, or say like: Welcome Tom "profile image" ? Thanks again. – Dave_O Dec 31 '14 at 17:37
  • You can use wp_get_current_user. – Rohil_PHPBeginner Jan 1 '15 at 6:22

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.