Skip to content

Instantly share code, notes, and snippets.

@gmmedia
Last active June 20, 2024 09:01
Show Gist options
  • Save gmmedia/01c040966da670a707f2845fb34413e4 to your computer and use it in GitHub Desktop.
Save gmmedia/01c040966da670a707f2845fb34413e4 to your computer and use it in GitHub Desktop.
Add alt tag to WordPress Gravatar images
<?php
// Add alt tag to WordPress Gravatar images
// Full how-to: https://bloggerpilot.com/gravatar-alt-tag/
function bloggerpilot_gravatar_alt($bloggerpilotGravatar) {
if (have_comments()) {
$alt = get_comment_author();
}
else {
$alt = get_the_author_meta('display_name');
}
$bloggerpilotGravatar = str_replace('alt=\'\'', 'alt=\'Avatar f&uuml;r ' . $alt . '\'', $bloggerpilotGravatar);
return $bloggerpilotGravatar;
}
add_filter('get_avatar', 'bloggerpilot_gravatar_alt');
@heatherine009
Copy link

Hi, I added this code to code snippets, but it doesn't seem to work? Should I add it to the root file instead? Thanks

@gmmedia
Copy link
Author

gmmedia commented Jun 20, 2024

You need to add it to your functions.php files in your child theme, or to a snippet plugin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment