Site logo

Archived topic

Missing Alt Text in Author Gravatar

3 replies · Started by Amit on October 15, 2021

Viewing posts 1–4 of 4

Hi,
I am using the following code for the Gravatar image -

/* Author Box Gravatar PHP */
function lh_page_hero_gravatar() {
    ob_start();
    global $post;
    $author_id = $post->post_author;
    ?>
    <div class="page-hero-gravatar">
        <?php echo get_avatar( $author_id ); ?>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode( 'page_hero_gravatar', 'lh_page_hero_gravatar' );
/* End Author Box Gravatar*/

In an audit, the gravatar image is missing alt tags, how can it be added?

Hi there,

try adding the following PHP Snippet:

function db_add_alt_to_gravatar($text) {
    $alt = get_the_author_meta( 'display_name' );
    $text = str_replace('alt=\'\'', 'alt=\'Avatar for '.$alt.'\' title=\'Gravatar for '.$alt.'\'',$text);
    return $text;
}
add_filter('get_avatar','db_add_alt_to_gravatar');

Works! Thanks, David for the Quickie...

Glad to be of help

This archived topic is closed to new replies.