WordPress Gravatar filter is removing my custom attributes

I am trying to add a style attribute on all the custom gravatar images. However, it is removing the style attribute before final output. Here is the code I am using:

$background = ”;
$args = [$background];

function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt, $args ) {
$user = false;

if ( is_numeric( $id_or_email ) ) {

$id = (int) $id_or_email;
$user = get_user_by( ‘id’ , $id );

} 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 );
}

$user_id = $user->user_email;

$image_hash = md5($user_id);
$first_color = substr($image_hash, 0, 6);
list($r, $g, $b) = sscanf($first_color, “%02x%02x%02x”);
$color_inner = “rgba($r, $g, $b, 0.25)”;
$color_outer = “rgba($r, $g, $b, 1)”;
$avatar = ‘https://www.gravatar.com/avatar/’.$image_hash.’?d=robohash’;
$args[0] = “background: radial-gradient($color_inner, $color_outer);”;
$avatar = “<img src='{$avatar}’ style='{$args[0]}’ class=’avatar avatar-{$size} photo’ height='{$size}’ width='{$size}’ />”;
// var_dump($avatar);

return $avatar;
}
add_filter( ‘get_avatar’ , ‘my_custom_avatar’ , 1 , 10, 6 );

If I uncomment var_dump($avatar), it gives me the following output:

<img src=”https://www.gravatar.com/avatar/image_hash?d=robohash” style=”background: radial-gradient(rgba(229, 24, 243, 0.25), rgba(229, 24, 243, 1));” class=”avatar avatar-64 photo” width=”64″ height=”64″>

However, the returned value of $avatar is:

<img src=”https://www.gravatar.com/avatar/image_hash?d=robohash” alt=”Comment Author” class=”avatar avatar-64 wp-user-avatar wp-user-avatar-64 photo avatar-default” width=”64″ height=”64″>

There is no code between var_dump($avatar) and return $avatar. What is causing this change in returned HTML? How can I get a return avatar with all attributes including style?

Read more here:: WordPress Gravatar filter is removing my custom attributes

Leave a Reply

Your email address will not be published. Required fields are marked *