I'm running wp multisite and using ACF custom field (user edit) for my local avatar, in my functions.php i added :
add_action('get_avatar', 'tsm_acf_profile_avatar', 10, 3);
function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$user = '';
// Get user by id or email
if ( is_numeric( $id_or_email ) ) {
$id = (int) $id_or_email;
$user = get_user_by( 'id' , $id );
} elseif ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->user_id ) ) {
$id = (int) $id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}
} else {
$user = get_user_by( 'email', $id_or_email );
}
if ( ! $user ) {
return $avatar;
}
// Get the user id
$user_id = $user->ID;
// Get the file id
$image_id = get_user_meta($user_id, 'avatar', true); // CHANGE TO YOUR FIELD NAME
// Bail if we don't have a local avatar
if ( ! $image_id ) {
return $avatar;
}
// Get the file size
$image_url = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name
// Get the file url
$avatar_url = $image_url[0];
// Get the img markup
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
}
This works great on main blog (blog 1), but problem is when i add that users on subsite(blog 2), my avatar field is empty in blog 2, also after that, if I upload avatar on blog 2, then same user on blog 1 get some random image of some random user.
I have no clue why this is happening, any help is welcome.
Thank you
I'm running wp multisite and using ACF custom field (user edit) for my local avatar, in my functions.php i added :
add_action('get_avatar', 'tsm_acf_profile_avatar', 10, 3);
function tsm_acf_profile_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
$user = '';
// Get user by id or email
if ( is_numeric( $id_or_email ) ) {
$id = (int) $id_or_email;
$user = get_user_by( 'id' , $id );
} elseif ( is_object( $id_or_email ) ) {
if ( ! empty( $id_or_email->user_id ) ) {
$id = (int) $id_or_email->user_id;
$user = get_user_by( 'id' , $id );
}
} else {
$user = get_user_by( 'email', $id_or_email );
}
if ( ! $user ) {
return $avatar;
}
// Get the user id
$user_id = $user->ID;
// Get the file id
$image_id = get_user_meta($user_id, 'avatar', true); // CHANGE TO YOUR FIELD NAME
// Bail if we don't have a local avatar
if ( ! $image_id ) {
return $avatar;
}
// Get the file size
$image_url = wp_get_attachment_image_src( $image_id, 'thumbnail' ); // Set image size by name
// Get the file url
$avatar_url = $image_url[0];
// Get the img markup
$avatar = '<img alt="' . $alt . '" src="' . $avatar_url . '" class="avatar avatar-' . $size . '" height="' . $size . '" width="' . $size . '"/>';
// Return our new avatar
return $avatar;
}
This works great on main blog (blog 1), but problem is when i add that users on subsite(blog 2), my avatar field is empty in blog 2, also after that, if I upload avatar on blog 2, then same user on blog 1 get some random image of some random user.
I have no clue why this is happening, any help is welcome.
Thank you
Did you try something like this?
User variables
<?php
// Define user ID
// Replace NULL with ID of user to be queried
$user_id = NULL;
// Example: Get ID of current user
// $user_id = get_current_user_id();
// Define prefixed user ID
$user_acf_prefix = 'user_';
$user_id_prefixed = $user_acf_prefix . $user_id;
?>
Display avatar
<?php $avatar = get_field( 'avatar', $user_id_prefixed ); ?>
<?php if ( $avatar ) { ?>
<img src="<?php echo $avatar['url']; ?>" alt="<?php echo $avatar['alt']; ?>" />
<?php } ?>