Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Checking gravatar is there or not in wordpress
<?php
/* Function for check gravatar */
function check_avatar($user_email_id) {
$hash = md5(strtolower(trim($user_email_id)));
$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;
}
/* Getting user_id and email */
$current_user_id = get_current_user_id();
$user_info = get_userdata($current_user_id);
$user_email_id = $user_info->user_email;
/* Getting User first name and last name */
$u_first_name = $user_info->user_firstname;
$u_last_name = $user_info->user_lastname;
/* If true displays gravatar else display defaut imag */
if (check_avatar($user_email_id) == "TRUE") {
echo get_avatar($user_email_id, 32);
} else {
?>
<!-- Genarating Name text avatar -->
<div class="name"><?php echo $u_first_name; ?>&nbsp;<?php echo $u_last_name; ?></div>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment