Skip to content

Commit ee8da6c

Browse files
committed
fix a11y force empty alt for decorative images
1 parent 16ccad9 commit ee8da6c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

inc/Helpers/Formatting/Image.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
* @return string Return the markup of the image
3636
*/
3737
function get_the_image( int $image_id, array $attributes, array $settings = [] ): string {
38+
// When the caller passes 'alt' => '', it was overridden by filters/wp_get_attachment_image with the attachment alt from BO.
39+
// We remember the intent here and force empty alt in the final markup below so decorative images get alt="" as intended.
40+
$force_empty_alt = array_key_exists( 'alt', $attributes ) && '' === $attributes['alt'];
41+
3842
$attributes = wp_parse_args(
3943
$attributes,
4044
[
@@ -74,6 +78,11 @@ function get_the_image( int $image_id, array $attributes, array $settings = [] )
7478

7579
$image_markup = apply_filters( 'bea_theme_framework_the_image_markup', $image_markup, $image_id, $attributes, $settings );
7680

81+
// Force alt="" in markup when empty alt was requested (see $force_empty_alt above).
82+
if ( $force_empty_alt ) {
83+
$image_markup = preg_replace( '/\salt="[^"]*"/', ' alt=""', $image_markup );
84+
}
85+
7786
return $settings['before'] . $image_markup . $settings['after'];
7887
}
7988

0 commit comments

Comments
 (0)