Repository navigation
Fix Posts featured images missing alt text #3774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,42 @@ | |
| exit; | ||
| } | ||
|
|
||
| if ( ! function_exists( 'stackable_posts_ensure_featured_image_alt' ) ) { | ||
| /** | ||
| * Keep a non-empty featured image alt as-is. | ||
| * When alt is empty or missing, use the escaped post title. | ||
| * Untitled posts keep an empty alt so the attribute is still present. | ||
| * | ||
| * @param string $img_html | ||
| * @param string $post_title | ||
| * @return string | ||
| */ | ||
| function stackable_posts_ensure_featured_image_alt( $img_html, $post_title ) { | ||
| $has_alt = preg_match( '/(?:^|\s)alt=(["\'])(.*?)\1/', $img_html, $alt_match ); | ||
|
|
||
| if ( $has_alt && $alt_match[2] !== '' ) { | ||
| return $img_html; | ||
| } | ||
|
|
||
| $alt_value = $post_title === '' ? '' : esc_attr( $post_title ); | ||
|
|
||
| if ( $has_alt ) { | ||
| if ( $alt_value === '' ) { | ||
| return $img_html; | ||
| } | ||
| return str_replace( $alt_match[0], ' alt="' . $alt_value . '"', $img_html ); | ||
| } | ||
|
|
||
| $img_tag_pos = strpos( $img_html, '<img' ); | ||
| if ( false === $img_tag_pos ) { | ||
| return $img_html; | ||
| } | ||
|
|
||
| $insert_at = $img_tag_pos + 4; | ||
| return substr( $img_html, 0, $insert_at ) . ' alt="' . $alt_value . '"' . substr( $img_html, $insert_at ); | ||
| } | ||
| } | ||
|
|
||
| if ( ! function_exists( 'generate_render_item_from_stackable_posts_block' ) ) { | ||
| /** | ||
| * Given a post array, attributes, | ||
|
|
@@ -40,15 +76,25 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t | |
| if ( is_array( $match ) && count( $match ) > 0 ) { | ||
| // Only remove the style tag inside the image tag. | ||
| $new_img = preg_replace( '/style=\"[^\"]*\"\s?/', "", $match[ 0 ] ); | ||
| $featured_image = preg_replace( "/<img[^\>]*>/", $new_img, $thumbnail ); | ||
| $post_title = isset( $post['post_title'] ) ? $post['post_title'] : ''; | ||
| $new_img = stackable_posts_ensure_featured_image_alt( $new_img, $post_title ); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Blocker] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. PHP was reading The thumbnail swap now uses I added PHPUnit cases for titles with |
||
| // Treat the replacement as plain text. preg_replace would | ||
| // read $n / ${n} / \n in the post title as backreferences. | ||
| $featured_image = str_replace( $match[ 0 ], $new_img, $thumbnail ); | ||
| } | ||
| } | ||
|
|
||
| // If the featured_image is empty, remove the markup. | ||
| if ( empty( $featured_image ) ) { | ||
| $new_template = preg_replace( '/<figure[^>]*>(.*)<\/figure>/', '', $new_template ); | ||
| } else { | ||
| $new_template = preg_replace( '/<img[^>]*>/', $featured_image, $new_template ); | ||
| $new_template = preg_replace_callback( | ||
| '/<img[^>]*>/', | ||
| function() use ( $featured_image ) { | ||
| return $featured_image; | ||
| }, | ||
| $new_template | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: gambitph/Stackable
Length of output: 1178
Script executed:
Repository: gambitph/Stackable
Length of output: 2224
Web query:
WHATWG HTML specification duplicate attribute parse error new attribute removed first attribute keptResult:
Make the alt detection case-insensitive and whitespace-tolerant.
The regex in
stackable_posts_ensure_featured_image_altmisses valid alt attributes such asALT="Custom"oralt = "Custom". The helper then inserts a secondaltattribute at the start of the<img>tag. Browsers keep the first duplicate, so the title (or an empty alt for untitled posts) replaces the existing custom alt. This needs thumbnail markup that a filter has changed, so the impact is narrow.Suggested fix
Committable suggestion
Script executed:
Repository: gambitph/Stackable
Length of output: 9344
Script executed:
Repository: gambitph/Stackable
Length of output: 6004
Anchor the featured image alt match to real attributes.
The
altregex on line 18 matches anyalt="preceded by whitespace, including text inside another quoted attribute. With a title such astitle='x alt="" y', the helper rewrites the title and leaves the realaltunchanged. Parse the tag's attributes in sequence instead of matching the firstalt=substring.