Skip to content

Commit 2aca958

Browse files
committed
Add alt tag to original_image_without_srcset
1 parent 2f2691a commit 2aca958

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

plugins/webp-uploads/picture-element.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,12 @@ function webp_uploads_wrap_image_in_picture( string $image, string $context, int
8181
// Extract sizes using regex to parse image tag, then use to retrieve tag.
8282
$width = 0;
8383
$height = 0;
84+
$alt = '';
8485
$processor = new WP_HTML_Tag_Processor( $image );
8586
if ( $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) {
8687
$width = (int) $processor->get_attribute( 'width' );
8788
$height = (int) $processor->get_attribute( 'height' );
89+
$alt = (string) $processor->get_attribute( 'alt' );
8890
}
8991
$size_to_use = ( $width > 0 && $height > 0 ) ? array( $width, $height ) : 'full';
9092

@@ -124,10 +126,11 @@ function webp_uploads_wrap_image_in_picture( string $image, string $context, int
124126
continue;
125127
}
126128
$picture_sources .= sprintf(
127-
'<source type="%s" srcset="%s" sizes="%s">',
129+
'<source type="%s" srcset="%s" sizes="%s" alt="%s">',
128130
esc_attr( $image_mime_type ),
129131
esc_attr( $image_srcset ),
130-
esc_attr( $sizes )
132+
esc_attr( $sizes ),
133+
esc_attr( $alt )
131134
);
132135
}
133136

@@ -142,7 +145,15 @@ function webp_uploads_wrap_image_in_picture( string $image, string $context, int
142145
return false;
143146
};
144147
add_filter( 'wp_calculate_image_srcset_meta', $filter );
145-
$original_image_without_srcset = wp_get_attachment_image( $attachment_id, $original_sizes, false, array( 'src' => $original_image ) );
148+
$original_image_without_srcset = wp_get_attachment_image(
149+
$attachment_id,
150+
$original_sizes,
151+
false,
152+
array(
153+
'src' => $original_image,
154+
'alt' => $alt,
155+
)
156+
);
146157
remove_filter( 'wp_calculate_image_srcset_meta', $filter );
147158

148159
return sprintf(

0 commit comments

Comments
 (0)