How can I filter the user avatar displayed in comments? – get_avatar_url filter works everywhere but not in comments

By default, wp_list_comments() uses the get_avatar() function to display user avatars, which in turn retrieves the avatar URL from Gravatar. However, get_avatar() also applies the get_avatar_url filter before returning the URL, so you can modify the avatar URL for the comments by using that filter.

Here’s an example of how you can modify the avatar URL for comments using the get_avatar_url filter:

add_filter( 'get_avatar_url', 'my_plugin_filter_get_avatar_url', 10, 3 );
function my_plugin_filter_get_avatar_url( $url, $id_or_email, $args ) {
    // Check if we are displaying a comment avatar
    if ( is_object( $args ) && isset( $args->object_type ) && $args->object_type === 'comment' ) {
        // Do something to modify the avatar URL for comments
        return $modified_url;
    // Return the original avatar URL for other cases
    return $url;

In this example, we check if the object_type argument of the $args parameter is set to ‘comment’, which indicates that we are displaying a comment avatar. If that’s the case, we modify the avatar URL as desired, and return the modified URL. For other cases, we simply return the original URL.

Note that if you have other filters hooked to the get_avatar_url filter, they will also be applied to the comment avatars, so make sure your filter is compatible with other filters that may be running.