Skip Navigation

[Resolved] Replace WP Profile Image With User Field Image

This thread is resolved. Here is a description of the problem and solution.

Problem:

How to replace the default user avatar with a custom user image field I've created in Toolset.

Solution:

You can try with filter hook "get_avatar", for example:

https://toolset.com/forums/topic/replace-wp-profile-image-with-user-field-image/#post-1607793

Relevant Documentation:

https://developer.wordpress.org/reference/hooks/get_avatar/

This support ticket is created 4 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 2 voices.

Last updated by JoshuaJ301 4 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1606449

Tell us what you are trying to do?
I'm trying to replace the default user profile image with a custom user field I've created in Toolset.
Following Beda's solution on the ticket to create a post field and a user field, I've successfully been able to use a function that updates the user field with the same data as the post field for the profile image.

I found this code that originally uses ACF custom user_meta field, but when I insert my toolset user field in the place, I get no avatar. I'm wondering why this is the case since the case scenario is virtually the same here.

add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5);
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, 'wpcf-user-profile-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;
}

Is there any documentation that you are following?
https://toolset.com/forums/topic/insert-custom-photo-to-wp-profile-picture/
Is there a similar example that we can see?

What is the link to your site?
hidden link

#1606755

Hello,

The custom image field created with Toolset Types plugin stores image URL value in database, you can try to modify your PHP codes from:

// Get the file id
    $image_id = get_user_meta($user_id, 'wpcf-user-profile-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];

To:

// Get the file URL
    $image_url  = get_user_meta($user_id, 'wpcf-user-profile-avatar', true); 

And test again

#1607693

Hi Luo,

Thanks for the reply. I did swap out that code as you suggested. When I cleared the cache it seems like the avatar is still showing up blank.

#1607793

Please try below codes, and test again:

add_filter('get_avatar', 'tsm_acf_profile_avatar', 10, 5);
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
    $avatar_url = get_user_meta($user_id, 'wpcf-user-profile-avatar', true);
    // 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;
}
#1607847

Thanks Luo,
That solved the problem for me. Now the avatars are synced with the toolset profile and wordpress.

Thank you very much for your help.

My issue is resolved now. Thank you!