Don't Send Guest Commenters' Emails to Gravatar
Shows a built-in placeholder avatar for comments from visitors without an account, so their hashed email address is never sent to Gravatar, while registered users keep their Gravatar picture.
Opens WPIDE on
Code
add_filter( 'pre_get_avatar_data', function ( $args, $id_or_email ) {
$is_guest = false;
if ( $id_or_email instanceof WP_Comment ) {
$is_guest = ! (int) $id_or_email->user_id;
} elseif ( is_string( $id_or_email ) && is_email( $id_or_email ) ) {
$is_guest = ! email_exists( $id_or_email );
}
if ( ! $is_guest ) {
return $args;
}
$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64"><rect width="64" height="64" fill="#c3c4c7"/><circle cx="32" cy="25" r="12" fill="#fff"/><path d="M12 58c2-13 10-19 20-19s18 6 20 19z" fill="#fff"/></svg>';
$args['url'] = 'data:image/svg+xml;base64,' . base64_encode( $svg );
$args['found_avatar'] = true;
return $args;
}, 10, 2 );
Notes
Change the color in the SVG to match your design; registered users can still be shown with their Gravatar.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.