Skip to content

Instantly share code, notes, and snippets.

@norcross
Created October 4, 2012 00:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save norcross/3830855 to your computer and use it in GitHub Desktop.
Save norcross/3830855 to your computer and use it in GitHub Desktop.
select random images for Gravatars
add_filter ( 'get_avatar', 'rkv_burt_avatar', 10, 5 );
function rkv_burt_avatar($avatar, $id_or_email, $size, $default, $alt) {
// we don't want this running anywhere on admin. no real reason.
if (is_admin() )
return $avatar;
// making sure we don't hit any of the avatars in the menu bar or other weird spots
if (isset($id_or_email) && $id_or_email !== 1) {
// commenters
$email = $id_or_email->comment_author_email;
$exist = rkv_validate_gravatar($email);
} else {
// this is for folks with user accounts on the site
$u_id = get_current_user_id();
$email = get_the_author_meta( 'user_email', $u_id );
$exist = true;
}
if($exist == true ) {
$grav_img = 'http://www.gravatar.com/avatar/'.md5(strtolower($email)) . '?d=' . urlencode($default) . '&s=' . $size;
$gravatar = '<img src="'.$grav_img.'"/>';
} else {
$burt_img = gimme_burt();
$gravatar = '<img src="'.get_stylesheet_directory_uri().'/images/burt/'.$burt_img.'" height="'.$size.'" width="'.$size.'"/>';
}
return $gravatar;
}
// function for checking on the existance of a gravatar
function rkv_validate_gravatar($email) {
// Craft a potential url and test its headers
$hash = md5(strtolower(trim($email)));
$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
$headers = @get_headers($uri);
if (!preg_match("|200|", $headers[0])) {
$has_valid_avatar = false;
} else {
$has_valid_avatar = true;
}
return $has_valid_avatar;
}
// function for scanning the folder with the images to be used
function gimme_burt() {
$burt_array = scandir( get_stylesheet_directory(__FILE__) . '/images/burt' );
$burt_faces = array_filter( $burt_array, create_function('$x','return in_array(strtolower(substr($x,-3)),array("png","jpg","gif"));'));
$burt_face = array_rand(array_flip($burt_faces), 1);
return $burt_face;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment