How to replace any occurence of Gravatars with a local placeholder image?

OK, so there are some occurrences of get_avatar() in your site. If you’ll take a look at docs for this function, you’ll see, that:

  • you pass URL for default avatar image as 3rd param,
  • you pass args as 5th param.

And one of these args is:

force_default (bool) (optional) Whether to always show the default
image, never the Gravatar. Default: false.

And as default you can use:

URL for the default image or a default type. Accepts ‘404’ (return a
404 instead of a default image), ‘retro’ (8bit), ‘monsterid’
(monster), ‘wavatar’ (cartoon face), ‘indenticon’ (the “quilt”),
‘mystery’, ‘mm’, or ‘mysteryman’ (The Oyster Man), ‘blank’
(transparent GIF), or ‘gravatar_default’ (the Gravatar logo). Default:
Default is the value of the ‘avatar_default’ option, with a fallback
of ‘mystery’.

But…

if there are a lot of occurrences of get_avatar or you don’t want to modify these occurrences (for example some of them come from plugins), then you can achieve this using filters.

One way to do this would be to use pre_get_avatar filter.

If you’ll return any non-null value, the rest of get_avatar function will get ignored and your result will be used as avatar. So you can use something like this, to do the trick:

function replace_all_avatars_with_default_one( $html, $id_or_email, $args ) {
    $url="<URL TO DEFAULT AVATAR";
    $class = array();
    return $avatar = sprintf(
        "<img alt="https://wordpress.stackexchange.com/questions/328986/%s" src="https://wordpress.stackexchange.com/questions/328986/%s" class="https://wordpress.stackexchange.com/questions/328986/%s" height="%d" width="%d" %s/>",
        esc_attr( $args['alt'] ),
        esc_url( $url ),
        esc_attr( join( ' ', $class ) ),
        (int) $args['height'],
        (int) $args['width'],
        $args['extra_attr']
    );
}
add_filter( 'pre_get_avatar', 'replace_all_avatars_with_default_one', 10, 3 );