Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Remove SRCSET from FV Gravatar Cache WordPress

  • SOLVED

Hello experts.

I've installed a plugin called FV Gravatar Cache which caches gravatar icon.

It works, but there's one thing that I'd like to change which is removing SRCSET and its URL from the output. Currently the full output is as follow:

<img alt='' src='//bd23.https.cdn.softlayer.net/80BD23/142.4.51.106/blog/wp-content/uploads/fv-gravatar-cache/f0c7619c69d4ba204497292e41259f65x80.png' srcset='https://secure.gravatar.com/avatar/f0c7619c69d4ba204497292e41259f65?s=160&amp;d=mm&amp;r=g 2x' class='avatar avatar-80 photo' height='80' width='80' />

So I'd like exclude "srcset='https://secure.gravatar.com/avatar/f0c7619c69d4ba204497292e41259f65?s=160&amp;d=mm&amp;r=g 2x'" from the output.

You can see the live example at my website: https://www.raymond.cc/blog/disable-avg-zen-and-uninstall-avira-connect-launcher/#comments

Thanks.

Additional Information:

I found that if I modify the Wordpress core file pluggable.php from wp-includes, removing srcset='%s' from line 2395 and also removing esc_attr( "$url2x 2x" ), then it will no longer show the srcset.

I am looking for a more elegant way to remove the srcset rather than modifying the core files.

Answers (3)

2017-04-06

Reigel Gallarde answers:

you can try this on your functions.php

if( get_option( 'fv_gravatar_cache') ) {
add_filter( 'get_avatar', 'custom_get_avatar', 11);
}

function custom_get_avatar( $image ) {

if( is_admin() ){
return $image;
}

$image = preg_replace( '/srcset=(["\'])[^\1]*?\1/i', '', $image, -1 );

return $image;
}


raymond comments:

Added your code to functions.php but the srcset of gravatar is still there.


Reigel Gallarde comments:

I change the preg_replace.. it was wrong... please check now...


Reigel Gallarde comments:

this should be the correct preg_replace...

$image = preg_replace( '/srcset=(["\'])[^\1]*?\1/i', '', $image, -1 );


I updated above to reflect to this...


raymond comments:

Thanks it works perfectly.

2017-04-06

Arnav Joy answers:

please write this jquery in footer.php of your theme

<script>
jQuery(document).ready(function($){
$('img.avatar').removeAttr('srcset');​
});
</script>


raymond comments:

Thanks but sorry I am looking for a server side solution.
I don't want to rely on a browser side solution such as using Javascript to remove it.

2017-04-06

Hiro Hito answers:

You can edit the plugin and remove the attribute directly, but the edits will be overwritten if you update it in future