Skip to content

Instantly share code, notes, and snippets.

@billerickson
Last active October 14, 2016 08:11
Show Gist options
  • Save billerickson/1325548 to your computer and use it in GitHub Desktop.
Save billerickson/1325548 to your computer and use it in GitHub Desktop.
Use Custom Avatar if Provided
<?php
/**
* Use Custom Avatar if Provided
* @author Bill Erickson
* @link http://www.billerickson.net/wordpress-custom-avatar/
*
*/
function be_gravatar_filter($avatar, $id_or_email, $size, $default, $alt) {
// If provided an email and it doesn't exist as WP user, return avatar since there can't be a custom avatar
$email = is_object( $id_or_email ) ? $id_or_email->comment_author_email : $id_or_email;
if( is_email( $email ) && ! email_exists( $email ) )
return $avatar;
$custom_avatar = get_the_author_meta('be_custom_avatar');
if ($custom_avatar)
$return = '<img src="'.$custom_avatar.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
elseif ($avatar)
$return = $avatar;
else
$return = '<img src="'.$default.'" width="'.$size.'" height="'.$size.'" alt="'.$alt.'" />';
return $return;
}
add_filter('get_avatar', 'be_gravatar_filter', 10, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment