Display avatar of user profile when logged in
-
10-05-2021 - |
Question
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.
Solution
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 );
}
}
OTHER TIPS
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;
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange