There is a little bit of explaining needed here. Because the avatar is associated with user meta and the user meta table is shared in multisite installations, the meta key includes the table prefix and blog id followed by user_avatar
. On non-multisite installations the blog id is omitted.
For example, for a default, unaltered, non-multisite installation for which the table prefix was left as wp_
the meta key is wp_user_avatar
.
If say the table prefix was changed to mysite_
the meta key becomes mysite_user_avatar
.
If this were a multisite installation and the blog id is 4
the meta key becomes mysite_4_user_avatar
.
Because this is associated with the user meta table, the function to retrieve the avatar is get_user_meta()
. The easiest way to retrieve the meta key is to use the $wpdb->get_blog_prefix()
method.
Note that the meta value stores the avatar id and not the path. For this reason you need to use the wp_get_attachment_image_src()
function to retrieve the avatar URL.
Here is a working example:
global $wpdb, $blog_id;
$avatar_id = get_user_meta( $user_id, $wpdb->get_blog_prefix( $blog_id ) . 'user_avatar', true );
$avatar_image = wp_get_attachment_image_src( $avatar_id, 'full' );
$avatar_url = $avatar_image[0];