Write Comments Under a Different WordPress User and Gravatar Photo

Updated 02/02/2024, Posted 02/02/2024 by James Parsons James Parsons 0 Comments

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!

Related Code Snippets

Written by James Parsons

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.