From ee8da6c98696e5632aff2c744a9dca090ea3544b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ce=CC=81dric=20Andrietti?= Date: Fri, 6 Mar 2026 15:28:06 +0100 Subject: [PATCH] fix a11y force empty alt for decorative images --- inc/Helpers/Formatting/Image.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/inc/Helpers/Formatting/Image.php b/inc/Helpers/Formatting/Image.php index 85424b96..55b49ac2 100644 --- a/inc/Helpers/Formatting/Image.php +++ b/inc/Helpers/Formatting/Image.php @@ -35,6 +35,10 @@ * @return string Return the markup of the image */ function get_the_image( int $image_id, array $attributes, array $settings = [] ): string { + // When the caller passes 'alt' => '', it was overridden by filters/wp_get_attachment_image with the attachment alt from BO. + // We remember the intent here and force empty alt in the final markup below so decorative images get alt="" as intended. + $force_empty_alt = array_key_exists( 'alt', $attributes ) && '' === $attributes['alt']; + $attributes = wp_parse_args( $attributes, [ @@ -74,6 +78,11 @@ function get_the_image( int $image_id, array $attributes, array $settings = [] ) $image_markup = apply_filters( 'bea_theme_framework_the_image_markup', $image_markup, $image_id, $attributes, $settings ); + // Force alt="" in markup when empty alt was requested (see $force_empty_alt above). + if ( $force_empty_alt ) { + $image_markup = preg_replace( '/\salt="[^"]*"/', ' alt=""', $image_markup ); + } + return $settings['before'] . $image_markup . $settings['after']; }