Issue
I have a feature that allows users to upload their own images as avatars. The uploaded images are stored correctly in the meta_key named image
. So it works.
The point is that uploaded images don't show up in the comments section, admin panel, or anywhere on the site. This is because wordpress does not use the image
meta_key to display images, but it does with the get_avatar
filter.
What I am trying to do is set the meta_key image
in the get_avatar
filter, this way the user avatars should be visible in the comments section, reviews and everywhere on the site.
I'm trying to run the code below and it only works for the admin section, so in the backend I can actually see the avatars uploaded by each user. But gravatar's default images still appear everywhere in the front-end of the site.
What am I doing wrong ?
// Replace default Gravatar Image used in WordPress
add_filter( 'get_avatar', 'filter_get_avatar', 10, 5 );
function filter_get_avatar( $avatar, $current_user, $size, $default, $alt ) {
// If is email, try and find user ID
if ( ! is_numeric( $current_user ) && is_email( $current_user->comment_author_email ) ) {
$user = get_user_by( 'email', $current_user );
if ( $user ) {
$current_user = get_current_user_id();
}
}
// If not user ID, return
if( ! is_numeric( $current_user ) ) {
return $avatar;
}
// Get attachment id
$attachment_id = get_user_meta( $current_user, 'image', true );
// NOT empty
if ( ! empty ( $attachment_id ) ) {
// Return saved image
return wp_get_attachment_image( $attachment_id, [ $size, $size ], false, ['alt' => $alt] );
}
return $avatar;
}
Update:
In this way the images are displayed in the comments section, admin and I believe also elsewhere on the site. But the problem is that all users get the same image. What am I doing wrong ?
function wdoc_filter_get_avatar_url( $url, $id_or_email, $args ) {
$current_user_id = get_current_user_id();
$file_id = get_user_meta( $current_user_id, 'image' , true );
return wp_get_attachment_image_url($file_id);
}
add_filter( 'get_avatar_url', 'wdoc_filter_get_avatar_url', 10, 3 );
Solution
I'm posting this answer because the solution below is working for me. I'm not a developer, but a fan working on their own website. For this reason, the code below may contain errors. If anyone wants to make improvements corrections are welcome, I would appreciate it very much. For now it works for me.
It helped me solve the problem: https://rocketgeek.com/code-snippets/custom-avatar-image-based-on-image-field-in-wp-members/
Thanks to CBroe for the tips he suggested to me.
add_filter( 'get_avatar_url', 'my_custom_avatar_author', 10, 3 );
function my_custom_avatar_author( $avatar, $id_or_email, $size ) {
// What is the custom image field's meta key?
// Set this value to match the meta key of your custom image field.
$meta_key = "image";
// Nothing really to change below here, unless
// you want to change the <img> tag HTML.
$user = false;
if ( is_numeric( $id_or_email ) ) {
$user = get_user_by( 'id' , (int)$id_or_email );
}
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 && is_object( $user ) ) {
$post_id = get_user_meta( $user->ID, $meta_key, true );
if ( $post_id ) {
$attachment_url = wp_get_attachment_url( $post_id );
$avatar = wp_get_attachment_image_url($post_id, $size = array('50', '50')); // HTML for the avatar <img> tag. This is WP default.
}
}
return $avatar;
}
Answered By - Snorlax Answer Checked By - Clifford M. (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.