1

I'm using Add a profile picture (file upload) on My account > edit account in WooCommerce answer code to a add custom image upload field.

However, this uploaded image is only displayed on a user's personal profile in WooCommerce.

I would like those images also to be visible to the admin in the backend, on the user list that appears in the Wordpress dashboard (see attached images).

enter image description here

enter image description here


So the intention is to change the user profile picture by the uploaded image.

How can I solve it?

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
liming
  • 232
  • 2
  • 4
  • 18

1 Answers1

4

By default Gravatar Image in used in WordPress. So based on:

Questions and answers, you will get this updated code, which will answer your question:

// Replace default Gravatar Image used in WordPress
function filter_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {    
    // If is email, try and find user ID
    if ( ! is_numeric( $id_or_email ) && is_email( $id_or_email->comment_author_email ) ) {
        $user = get_user_by( 'email', $id_or_email );
        if ( $user ) {
            $id_or_email = $user->ID;
        }
    }

    // If not user ID, return
    if( ! is_numeric( $id_or_email ) ) {
        return $avatar;
    }

    // Get attachment id
    $attachment_id  = get_user_meta( $id_or_email, '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;
}
add_filter( 'get_avatar', 'filter_get_avatar', 10, 5 );

WordPress functions used:

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50