plugin development - User avatar-ACF fields

admin2025-01-07  5

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

Share Improve this question asked May 25, 2018 at 2:49 MR.Don't knowMR.Don't know 115 bronze badges 1
  • I'd guess the problem is that user meta is shared across multisites but attachment IDs are unique per site, so user 1 has avatar attachment ID 123 on the first site, but on the second site some other user's avatar is attachment ID 123. – Rup Commented Aug 23, 2023 at 13:26
Add a comment  | 

1 Answer 1

Reset to default 0

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 } ?>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736252737a69.html

最新回复(0)