Snippi
A super awesome snippet tool.
- 1.
- 2.
/**
- 3.
* Replace Gravatar with custom image URL in wp_usermeta - photo_url
- 4.
* cf. Mike Hemberger http://thestizmedia.com/acf-pro-simple-local-avatars/ and https://twitter.com/JiveDig/status/706138742604570624
- 5.
*/
- 6.
- 7.
add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5);
- 8.
function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
- 9.
- 10.
// Get user by id or email
- 11.
if ( is_numeric( $id_or_email ) ) {
- 12.
$id = (int) $id_or_email;
- 13.
$user = get_user_by( 'id' , $id );
- 14.
} elseif ( is_object( $id_or_email ) ) {
- 15.
if ( ! empty( $id_or_email->user_id ) ) {
- 16.
$id = (int) $id_or_email->user_id;
- 17.
$user = get_user_by( 'id' , $id );
- 18.
}
- 19.
} else {
- 20.
$user = get_user_by( 'email', $id_or_email );
- 21.
}
- 22.
// Get the user id
- 23.
$user_id = $user->ID;
- 24.
- 25.
- 26.
// Get the file id
- 27.
$image_url = get_user_meta($user_id, 'photo_url', true); // CHANGE TO YOUR FIELD NAME
- 28.
// Bail if we don't have a local avatar
- 29.
if ( ! $image_url ) {
- 30.
return $avatar;
- 31.
}
- 32.
- 33.
// Get the img markup
- 34.
$avatar = '<img alt="' . $alt . '" src="' . $image_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
- 35.
// Return our new avatar
- 36.
return $avatar;
- 37.
- 38.
- 39.
}
- 40.