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
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
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.
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;
}
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!