Home / Attachments / Add default ALT to avatar/Gravatar Images
Duplicate Snippet

Embed Snippet on Your Site

Add default ALT to avatar/Gravatar Images

Add the user's name as a default alt tag to the Gravatar images loaded on...

800+
Code Preview
php
<?php
add_filter(
	'pre_get_avatar_data',
	function ( $atts ) {
		if ( empty( $atts['alt'] ) ) {
			if ( have_comments() ) {
				$author = get_comment_author();
			} else {
				$author = get_the_author_meta( 'display_name' );
			}
			$alt = sprintf( 'Avatar for %s', $author );
			$atts['alt'] = $alt;
		}
		return $atts;
	}
);

Comments

Add a Comment