I am using a plugin that allows users to upload profile graphics in place of avatars. The plugin is "User Avatar".
I am displaying user profile graphics with this code:
echo get_avatar( get_the_author_meta('user_email'), $size = '60')
It works fine except I would like it to function so that, if a user does not have a user profile graphic, it defaults to display nothing. If I use the avatar Discussions settings to display "Blank" when there is no user avatar I still get text that is wrapped around where the graphics would be, creating an empty square where a user profile should otherwise be.
Does anyone have any suggestions?
Thank you in advance for considering.
I am using a plugin that allows users to upload profile graphics in place of avatars. The plugin is "User Avatar".
I am displaying user profile graphics with this code:
echo get_avatar( get_the_author_meta('user_email'), $size = '60')
It works fine except I would like it to function so that, if a user does not have a user profile graphic, it defaults to display nothing. If I use the avatar Discussions settings to display "Blank" when there is no user avatar I still get text that is wrapped around where the graphics would be, creating an empty square where a user profile should otherwise be.
Does anyone have any suggestions?
Thank you in advance for considering.
A few months ago I wrote a discussion over at this WordPress.org thread regarding this. Check it out: http://wordpress.org/support/topic/custom-avatar-4?replies=5
Here is the code you'll need in the template you want your avatars:
<?php
$uploads = wp_upload_dir(); //WordPress upload path
$uploads_url = ( $uploads['baseurl'] ); //full url of upload dir
$uploads_dir = ( $uploads['basedir'] ); //full path of upload dir
$user_id = $post->post_author; //id of author (if used in loop)
$avatar_filename = user_avatar_avatar_exists($user_id ); //function from plugin
if (file_exists($uploads_dir.'/avatars/'.$user_id .'/'.$avatar_filename)) {
echo ('<img src='.$uploads_dir.'/avatars/'.$user_id .'/'.$avatar_filename.' class="user-avatar"/>');
}
else {
echo get_avatar( get_the_author_meta('user_email'), $size = '60');
} ?>
EDIT: To show nothing when the gravar is empty use this (source above thread):
<?php
$uploads = wp_upload_dir(); //WordPress upload path
$uploads_url = ( $uploads['baseurl'] ); //full url of upload dir
$uploads_dir = ( $uploads['basedir'] ); //full path of upload dir
$user_id = $post->post_author; //id of author (if used in loop)
$avatar_filename = user_avatar_avatar_exists($user_id ); //function from plugin
$hash = md5(strtolower(trim($user->user_email)));
$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
$headers = @get_headers($uri);
if (((preg_match("|200|", $headers[0])) || (file_exists($uploads_dir.'/avatars/'.$user_id .'/'.$avatar_filename))) {
if (file_exists($uploads_dir.'/avatars/'.$user_id .'/'.$avatar_filename)) {
echo ('<img src='.$uploads_dir.'/avatars/'.$user_id .'/'.$avatar_filename.' class="user-avatar"/>');
}
else {
echo get_avatar( get_the_author_meta('user_email'), $size = '60');
}
} ?>
Here is the filter that will do the trick:
function ns_filter_avatar($avatar, $id_or_email, $size, $default, $alt, $args)
{
$headers = @get_headers( $args['url'] );
if( ! preg_match("|200|", $headers[0] ) ) {
return;
}
return $avatar;
}
add_filter('get_avatar','ns_filter_avatar', 10, 6);
To work you have to add value 404
as third argument $default
to get_avatar()
. Example:
get_avatar( $user_email, 45, '404' )