Wordpress - Replace Gravatar with uploaded images?
Presuming that the user has their avatar saved, as the ID of an attachment, store in user meta, as the field field_with_custom_avatar_id
, you could do this to show that attachment if the value is saved:
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find ID of attachment saved user meta
$saved = get_user_meta( $id_or_email, 'field_with_custom_avatar_id', true );
if( 0 < absint( $saved ) ) {
//return saved image
return wp_get_attachment_image( $saved, [ $size, $size ], false, ['alt' => $alt] );
}
//return normal
return $avatar;
}
Or, if it is saved as a URL of the image, in the user meta field field_with_custom_avatar
-
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find URL of saved avatar in user meta
$saved = get_user_meta( $id_or_email, 'field_with_custom_avatar', true );
//check if it is a URL
if( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
//return saved image
return sprintf( '<img src="%" alt="%" />', esc_url( $saved ), esc_attr( $alt ) );
}
//return normal
return $avatar;
}
The hook you need is the get_avatar
filter. It returs the image HTML element representing the user avatar.
add_filter( 'get_avatar', 'cyb_get_avatar', 10, 5 );
function cyb_get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
// Replace $avatar with your own image element, for example
// $avatar = "<img alt='$alt' src='your_new_avatar_url' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"
return $avatar;
}
Note that using this filter, you can still let users use gravatar. You could check if the user has uploaded an avatar to your site, then use it, it not you return normal $avatar
, which will be from gravatar if user has one. (If you add to question the code you are using to store user avatars, I can give a exact working code):
add_filter( 'get_avatar', 'cyb_get_avatar', 10, 5 );
function cyb_get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
if( "user_has_uploaded_a_local_avatar" ) {
// Replace $avatar with your own image element, for example
// $avatar = "<img alt='$alt' src='your_new_avatar_url' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"
}
// if user didn't upload a local avatar,
// normal avatar will be used, which can be from gravatar
return $avatar;
}