Write Comments Under a Different WordPress User and Gravatar Photo
We manage some of our clients' blogs and ghostwrite for them, and occasionally, we respond to comments that those posts recieve.
Unfortunately some WordPress installs won't let us comment on behalf of another user. It will use our WordPress gravatar, even if we set our name and email to match the user that we want to comment as.
Thankfully, this handy little functions.php snippet fixes that:
/* Allow admin creation of comments under a different email and Gravatar */
function custom_gravatar_force_comment_email( $avatar, $id_or_email, $size, $default, $alt, $args ) {
if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
$comment = get_comment( $id_or_email->comment_ID );
if ( $comment ) {
$grav_url = "https://www.gravatar.com/avatar/" . md5(strtolower(trim($comment->comment_author_email))) . "?s=" . $size . "&d=mm";
$avatar = "<img alt='{$alt}' src='{$grav_url}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
}
return $avatar;
}
add_filter( 'get_avatar', 'custom_gravatar_force_comment_email', 9999, 6 );
Simply plop this into your functions.php file in your theme, and you can now change the email (and cooresponding gravatar) of any comment!
October 09, 2024
This snippet in functions.php totally works like magic for changing up comment Gravatars! What do you think about whether using this workaround might affect how well your site runs?
October 11, 2024
Hey Reynaldo!
It's really awesome to hear that the snippet actually worked well for you! Changing Gravatars can sometimes affect your website's loading speed especially if they're not fully optimized. I often look into things like this with some of my clients. If you ever see any slowdown it's definitely worth checking out your plugins as well.
As always let me know if you need any more advice! 😊
October 20, 2024
Hey! Thanks a lot for sharing this. It's super helpful to know all about that snippet.
October 30, 2024
Hey Tyrone!
I'm really happy to hear that you found it helpful! I've noticed that some of my clients use tools like Shopify to make their workflow easier and it really helps them manage their tasks better. You are more than welcome to reach out if you ever need any tips or advice. 📧