How to change avatar of the comment author using comment ID?

One visitor commented on one of my post as a guest. Now, i want to change avatar of this comment using comment ID. Comment ID of the comment is 1092. How can i change avatar of the comment using comment id?

Actually i want to change avatar for couple of comments.

Any help would be greatly appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use pre_get_avatar filter to manipulate comment avatar html and stop WP from getting a default avatar for the comment.

Passing a non-null value will effectively short-circuit get_avatar(),
passing the value through the ‘get_avatar’ filter and returning early.

E.g.

add_filter( 'pre_get_avatar', 'my_filter_pre_get_avatar', 10, 3 );
function my_filter_pre_get_avatar( $avatar_html, $id_or_email, $args ) {
    if ( is_a( $id_or_email, 'WP_Comment' ) ) {
        // Add more cases with elseif or use a switch statement here
        if ( '1092' === $id_or_email->comment_ID ) {
            // set avatar to whatever html you want
            $avatar_html = '<img class="avatar" src="" alt="Custom avatar">';
        }
    }
    return $avatar_html;
}

Use this in your theme’s functions.php file or in a custom plugin.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x