Remove URL Query String Parameters from Gravatars in WordPress

This snippet will remove the query string parameters from the source URL for Gravatar images in your comments section on your WordPress site.

By default, WordPress blog comments will show avatar images with a URL source like this:

The query string at the end of the Gravatar URLs for your site may be different, but it begins with the question mark (?), usually followed by s= to designate the size of the avatars.

Page speed tests will usually inform you that this URL is not cached because it has a “?” in the URL.

The following code will remove the query string. Please note that the following code will cause certain changes to the Gravatar images. First, the Gravatar image will be 80px by 80px. Second, the default Gravatar image (which is used for people that don’t have their own Gravatar) will be the blue Gravatar logo. Third, the highest rating allowed will be “G” which means that only Gravatar images with a rating of “G” will be allowed. Images rated PG, R, or X will defer back to the default blue Gravatar logo image. If you are okay with these three changes, then proceed.

This code goes in your functions:

 * Remove query string parameters from Gravatars
add_filter( 'get_avatar_url', 'isa_avatar_remove_querystring' );
function isa_avatar_remove_querystring( $url ) {
	$url_parts = explode( '?', $url );
	return $url_parts[0];

Alternate Method, When Using get_avatar()

If you are using a custom callback function to customize your comments, then you should simply just replace the call to get_avatar() with a custom image element.

You’re probably using something like:

echo get_avatar( $comment, 100 );

to display the Gravatar images. If you want to display the Gravatar images without the query strings in the image source URL, then do not use echo get_avatar(). Instead, use the following. It will display the whole avatar img element, but without query strings:

// remove query string from Gravatars
$avatar_src = get_avatar_url( $comment );
// Remove query string parameters from gravatars @todo make Tutorial
$url_parts = explode( '?', $avatar_src );
$clean_src = $url_parts[0];
// Display the image
?><img alt='avatar' src='<?php echo esc_url( $clean_src ); ?>' class='avatar avatar-80 photo' height='80' width='80' /><?php

Also see these for how to remove other query strings in WordPress:

Remove Query Strings from Static Sources in WordPress
Remove Query String From SyntaxHighlighter CSS URLs.

See more: , ,

We've One Response

  1. December 6th, 2020 at 5:42 am

    Hey Isabel,
    thank you for your Guide!
    I have a question that might be related to this. I am unsure if you (or anybody) can Help me, but it seems like you are kind of an expert in this area 😉

    I am trying to add URL Query String Parameters to 2 specific Images on my WordPress Website. I tried this guide but it does not seem to work. I already contacted the author, but no response so far.
    Do you have an Idea how I could use this function, but only for 2 Images instead of all Image Sizes?

    Thanks in Advance


Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: