add_filter( 'get_avatar' , 'alt_name_avatar');
function alt_name_avatar( $avatar ) {
$alt = get_comment_author();
$avatar = str_replace('alt=''','alt='Avatar for '.$alt.'' title='Avatar for '.$alt.''',$avatar);
return $avatar;
}
This code works but throws an error.
PHP Notice: Trying to get property 'user_id' of non-object in .../wp-includes/comment-template.php on line 28 PHP Notice: Trying to get property 'comment_ID' of non-object in .../wp-includes/comment-template.php on line 48
How to fix.
P.S.
i use on all pages recent comments with Gravatar in the sidebar
Sorry for my English.
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 don’t check anywhere that you’re viewing a comment when you use get_comment_author();. The get_avatar() function is used in a lot of places in WordPress; your code seems to assume it’s only in use on comments.
Try this (the code is untested, but should work, I think):
add_filter( 'get_avatar' , 'alt_name_avatar');
function alt_name_avatar( $avatar ) {
if ( null === get_comment() ) {
// This isn't a comment.
return $avatar;
}
$alt = get_comment_author();
$avatar = str_replace('alt=''','alt='Avatar for '.$alt.'' title='Avatar for '.$alt.''',$avatar);
return $avatar;
}
There doesn’t seem to be a simple is_comment() check to see if we’re viewing a comment, so I’ve chosen to test get_comment(), which will return null if we’re not in a comment.
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