Custom Avatars for Wordpress Comments VIA Website URL?
-
10-05-2021 - |
题
I've tried a couple different wordpress plugins and solutions but nothing is quite what I'm looking for.
I want to adjust my theme/wordpress so that avatars for my Wordpress commenters are grabbed from the "website URL" that people fill in. Does anyone know how I would go about doing this?
解决方案
By default most of the time WordPress comments will include a "gravatar" (linked to the user's email address) when displaying comments, using WordPress built-in function get_avatar()
.
You can hook a custom function to the get_avatar
filter to return another image.
Something like this should do the trick for your use case: it filters 'get_avatar'
and returns an image with the comment author's website URL if it is set. Add this to your theme (or child theme) functions.php:
add_filter( 'get_avatar', 'wpse310726_custom_avatar', 1, 5 );
function wpse310726_custom_avatar( $avatar, $id_or_email, $size, $default, $alt = null ) {
$comment_author_url = get_comment_author_url();
if ( '' !== $comment_author_url ) {
$avatar = "<img alt='{$alt}' src='{$comment_author_url}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
}
return $avatar;
}