Skip to content

fix a11y force empty alt for decorative images#475

Merged
cedric07 merged 1 commit intomasterfrom
fix/force-empty-alt
Mar 9, 2026
Merged

fix a11y force empty alt for decorative images#475
cedric07 merged 1 commit intomasterfrom
fix/force-empty-alt

Conversation

@cedric07
Copy link
Contributor

@cedric07 cedric07 commented Mar 6, 2026

Fix des alt forcé vide qui étaient ignorés. Utile pour les images décoratives.

Forcer un alt vide lorsque 'alt' => ''est passé à the_image()
Quand on passait 'alt' => '' dans les attributs de l’image, la valeur n’était pas respectée : le filtre du framework bea_theme_framework_the_image_attributes (et/ou wp_get_attachment_image) continuait à utiliser l’alt de la pièce jointe renseigné en médiathèque.

On détecte maintenant le cas où l’appelant demande explicitement un alt vide et, après génération du markup, on remplace l’attribut alt dans le HTML par alt="", afin que les images décoratives (ex. dans les listes de cartes) aient bien un alt vide pour l’accessibilité.

Summary by Sourcery

Bug Fixes:

  • Preserve an explicitly empty alt attribute passed to image helper calls by overriding any attachment alt injected by filters.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 6, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures that when callers explicitly pass an empty alt attribute to get_the_image(), the generated image markup preserves alt="" for accessibility by tracking this intent and post-processing the final HTML.

Sequence diagram for get_the_image alt handling

sequenceDiagram
    participant Caller
    participant get_the_image
    participant WordPress

    Caller->>get_the_image: get_the_image(image_id, attributes, settings)
    activate get_the_image
    get_the_image->>get_the_image: Detect force_empty_alt (alt key exists and is empty)
    get_the_image->>get_the_image: wp_parse_args(attributes, defaults)
    get_the_image->>WordPress: wp_get_attachment_image(image_id, attributes)
    WordPress-->>get_the_image: <img ... alt="from_attachment" ...>
    get_the_image->>get_the_image: apply_filters(bea_theme_framework_the_image_markup)
    alt force_empty_alt is true
        get_the_image->>get_the_image: preg_replace alt="*" with alt=""
    end
    get_the_image-->>Caller: before + <img ... alt="" ...> + after
    deactivate get_the_image
Loading

Flow diagram for forcing empty alt in get_the_image

flowchart TD
    A[Start get_the_image] --> B[Check if attributes contains key alt]
    B -->|No| C[force_empty_alt = false]
    B -->|Yes and attributes alt is empty string| D[force_empty_alt = true]
    B -->|Yes and attributes alt is not empty| E[force_empty_alt = false]

    C --> F[Build attributes with wp_parse_args]
    D --> F
    E --> F

    F --> G[Generate image markup via wp_get_attachment_image]
    G --> H[Apply filter bea_theme_framework_the_image_markup]
    H --> I{force_empty_alt?}
    I -->|No| J[Return before + markup + after]
    I -->|Yes| K[Use preg_replace to set alt="" in markup]
    K --> J
Loading

File-Level Changes

Change Details Files
Preserve explicitly empty alt attributes in generated image markup for decorative images.
  • Introduce a $force_empty_alt flag that tracks when the caller provided 'alt' => '' in the image attributes.
  • After generating and filtering the image markup, conditionally rewrite any existing alt attribute in the HTML to alt="" when $force_empty_alt is true using a preg_replace.
  • Leave all other behavior of get_the_image(), including existing filters and attribute defaults, unchanged.
inc/Helpers/Formatting/Image.php

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The preg_replace pattern '/\salt="[^"]*"/' assumes double quotes, a leading space, and an existing alt attribute; consider making the regex more robust (e.g., handle missing alt, varying whitespace, or single quotes) or using a DOM/attribute filter to modify alt instead of string replacement.
  • Currently the replacement runs on the whole $image_markup; if there are multiple <img> tags in the markup, this could unintentionally alter all of them—consider scoping the change to the specific image or to the first occurrence only.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `preg_replace` pattern `'/\salt="[^"]*"/'` assumes double quotes, a leading space, and an existing `alt` attribute; consider making the regex more robust (e.g., handle missing alt, varying whitespace, or single quotes) or using a DOM/attribute filter to modify `alt` instead of string replacement.
- Currently the replacement runs on the whole `$image_markup`; if there are multiple `<img>` tags in the markup, this could unintentionally alter all of them—consider scoping the change to the specific image or to the first occurrence only.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@cedric07 cedric07 merged commit d2c2efb into master Mar 9, 2026
7 checks passed
@cedric07 cedric07 deleted the fix/force-empty-alt branch March 9, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants