1

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.

0

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');
} 

} ?>
  • Thank you so much for this! But I am having trouble getting to it to echo nothing ("") if the avatar username and file name do not exist. Should the above code do that or should I add an echo nothing statement somewhere? Thanks again! – user2591811 Sep 29 '13 at 18:24
  • The above code will not do that, it checks for the uploaded avatar then displays the gravatar (if they don't have a gravatar shows a mystery man) I have updated the code to use a suggesting in the linked thread. – Brooke. Sep 30 '13 at 7:31
0

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' )

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.