Support » Plugins » Hacks » get_avatar() returns an url with #038; instead of &

  • In the latest WordPress something has changed in the get_avatar() function because the output has changed and it’s no longer correct. Note that in 4.1.x everything was just fine.
    Now when i call the function get_avatar like this:

    $astring = get_avatar($author,$size);

    I get this as output:

    <img alt='' src='' srcset=' 2x' class='avatar avatar-100 photo' height='100' width='100' />

    as you can see the url in src has “&” instead of “&”. Instead the url in srcset it’s correct.
    Why does this happen? Am I doing something wrong? It’s a WordPress bug?
    I have temporarily fixed the problem with a preg_replace but it don’t like it as a permanent solution.

    EDIT: sadly for some reason the support forum replaces & with & in the code block (really funny) so you can’t see of what I am talking about. Just take a look at this screenshot please.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi giu1io – it looks like they did make some modifications to get_avatar in order to add the srcset attribute in 4.2.

    Additionally, in 4.1 they were using str_replace to swap out & # 0 3 8 ; with & a m p ;. They are no longer doing so.

    The reason the srcset and src values are different is that one is being escaped with the esc_url() function and the other is being escaped with the esc_attr() function. Both are technically correct.

    & # 0 3 8 ; and & a m p ; are just different references to the same character… you should be able to use them interchangeably.

    Do you have a specific case in mind where the url with & # 0 3 8 ; is not working correctly?

    Thread Starter giu1io


    Yes I know that they are just different references for the same character and if I open the URL in the browser it opens without problems. Sadly I am using the avatar on iOS and NSUrl doesn’t like & # 0 3 8 ; instead of &.
    I was wondering why the src and srcset where encoded differently, I get your explanation but I just doesn’t make sense to me why someone would encode & as an entity in a URL.
    So I thought I could be some sort of bug, but if you’re telling me that it’s a conscious decision than I guess I’ll adapt my code accordingly.

    I think this is a real bug in wp. get_avatar should return but it returns & # 0 3 8 ; instead of &. It breaks the “Settings > Discussion -> Default avatar” setting.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_avatar() returns an url with #038; instead of &’ is closed to new replies.