Write Comments Under a Different WordPress User and Gravatar Photo

James Parsons by James Parsons Updated Feb 2nd, 2024 4 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

James Parsons is the founder and CEO of Content Powered, a premier content marketing agency that leverages nearly two decades of his experience in content marketing to drive business growth. Renowned for founding and scaling multi-million dollar eCommerce businesses through strategic content marketing, James has become a trusted voice in the industry, sharing his insights in Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, Inc, and other leading publications. His background encompasses key roles across various agencies, contributing to the content strategies of major brands like eBay and Expedia. James's expertise spans SEO, conversion rate optimization, and effective content strategies, making him a pivotal figure in the industry.