• I’m trying to get the current logged in user avatar, but have experienced problems doing so with the information available online.
    I am using:
    $user = wp_get_current_user();
    $avatar = get_avatar_url($user->ID, ['size' => 128]);
    But sometimes I am getting an empty string even though the user has an avatar or connected with gravatar.

    Am I missing something

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello,

    You can directly use get_avtar() function for the output of avtar.

    echo get_avatar($user_id, 80);

    Ref : https://developer.wordpress.org/reference/functions/get_avatar/

    Hope this helps!

    Thanks.

    Thread Starter Tom Levy

    (@nimlevy)

    Would that give any different results?
    Is the function get_avatar not relying on the same value returned by the function get_avatar_url?

    Dion

    (@diondesigns)

    The get_avatar() function can be replaced by a plugin (“pluggable” in WP-speak), and it allows plugins to bypass default avatar processing with the pre_get_avatar hook. The get_avatar_url() function does neither, so if you’re using a plugin that replaces get_avatar() or uses pre_get_avatar, get_avatar_url() can return a different URL than the one in the IMG tag returned by get_avatar().

    Thread Starter Tom Levy

    (@nimlevy)

    Is there a way to get the correct (based on your input) URL without the <img> tag?
    Or is parsing the result the only solution?

    Dion

    (@diondesigns)

    If you want the URL to be valid on all sites, I think you’ll need to parse the <IMG> tag. If this is for your own site, it would be worth locating which plugin is expanding avatar functionality, and checking whether the plugin has its own hook to obtain the URL.

    It would be worth trying to use the get_avatar hook. The avatar URL is passed in the $args array. Here is the documentation:

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

    If a plugin replaces avatar processing, it’s very likely the plugin has made sure this hook works because it’s used by a number of other popular plugins.

    Thread Starter Tom Levy

    (@nimlevy)

    I see.

    Thank you very much

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Best way of getting the user’s avatar’ is closed to new replies.