Make WordPress Core

Opened 6 weeks ago

Last modified 5 weeks ago

#66248 new enhancement

No way to turn off importing alt text from image XMP metadata

Reported by: siliconforks Owned by:
Priority: normal Milestone: Awaiting Review
Component: Media Version:
Severity: normal Keywords: has-patch
Cc: Focuses: accessibility

Description

This is a follow-up to issue #63895. The fix for that issue created a new wp_get_image_alttext() function which parses images for XMP metadata and uses that to populate the alt text for the image. Currently wp_get_image_alttext() is called unconditionally - if you don't want this functionality, there does not appear to be any way to turn it off. You could use the wp_read_image_metadata filter to overwrite the alt text after wp_get_image_alttext() has already run, but it would be better to have a way to prevent wp_get_image_alttext() from running a bunch of unnecessary code in the first place.

I will submit a pull request to address this.

Change History (19)

This ticket was mentioned in ​PR #14024 on ​WordPress/wordpress-develop by ​@siliconforks.


6 weeks ago
#1

  • Keywords has-patch added

Returning a non-null value will cause wp_get_image_alttext() to return early with that value.

Trac ticket: https://core.trac.wordpress.org/ticket/66248

Use of AI Tools

AI assistance: No

This ticket was mentioned in ​PR #14063 on ​WordPress/wordpress-develop by ​@therssoftware.


6 weeks ago
#2

  • Keywords has-unit-tests added

Adds comprehensive unit test coverage for the pre_wp_get_image_alttext filter introduced in #66248 (​PR #14024).

The tests in tests/phpunit/tests/image/alttext.php cover:

  • Short-circuiting and returning custom alt text (e.g. from an external service or database).
  • Short-circuiting with an empty string '' to turn off alt text generation as documented.
  • Falling through to standard XMP parsing when null is returned.
  • Ensuring the $file path argument is passed to the filter callback.
  • Short-circuiting without requiring file existence or DOM extension availability.

Testing:

  • vendor/bin/phpcs src/wp-admin/includes/image.php tests/phpunit/tests/image/alttext.php: passed with 0 errors and 0 warnings.
  • vendor/bin/phpunit tests/phpunit/tests/image/alttext.php: all 12 tests (17 assertions) pass cleanly on both single site and multisite.

Trac ticket: https://core.trac.wordpress.org/ticket/66248

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini
Used for: Drafting unit tests and test matrix for pre_wp_get_image_alttext. Code and test execution were reviewed and verified locally by robelsust.

#3 @therssoftware
6 weeks ago

  • Keywords has-unit-tests removed

Unit Tests for pre_wp_get_image_alttext

Added dedicated unit test coverage in ​PR #14063 (following up on ​PR #14024) to fulfill the needs-unit-tests requirement.

The tests in tests/phpunit/tests/image/alttext.php cover:

  • Custom string short-circuit: Returning a non-null string from pre_wp_get_image_alttext returns that string immediately.
  • Disable alt text extraction: Returning an empty string '' short-circuits and returns an empty string without parsing the file, as documented in the DocBlock.
  • Null fall-through: Returning null allows normal XMP parsing to execute.
  • Parameter passing: Verifies that the $file path argument is forwarded to the filter callback.
  • Bypass file/DOM requirement: Verifies that short-circuiting occurs before file reads or DOM extension checks.

All 12 tests (17 assertions) in tests/phpunit/tests/image/alttext.php pass cleanly on both single site and multisite. PHPCS passes with 0 errors and 0 warnings.

Props therssoftware.


AI Disclosure: In accordance with the WordPress AI policy, I disclose that generative AI (Google Antigravity) was used in drafting and verifying these unit tests. All test executions and code reviews were performed locally in the development environment.

This ticket was mentioned in ​Slack in #core by amykamala. ​View the logs.


6 weeks ago

#5 @afercia
6 weeks ago

  • Focuses accessibility added

#6 follow-ups: @jorbin
6 weeks ago

  • Focuses accessibility removed

if you don't want this functionality, there does not appear to be any way to turn it off.

What are the reasons that you wouldn't want this alt text to be imported if it already exists on the image?

#7 @jorbin
6 weeks ago

  • Focuses accessibility added

​@mppellicer commented on ​PR #14063:


6 weeks ago
#8

The PR says it only adds tests for a filter "introduced in PR #14024". But the diff also contains the filter implementation itself.

​@therssoftware commented on ​PR #14063:


6 weeks ago
#9

Thanks @mppellicer! The filter implementation from PR #14024 was included in this branch so that the unit tests could run and be verified in CI. If PR #14024 is merged first, I will gladly rebase this PR so it only contains the unit test file.

#10 in reply to: ↑ 6 @therssoftware
6 weeks ago

Replying to jorbin:

if you don't want this functionality, there does not appear to be any way to turn it off.

What are the reasons that you wouldn't want this alt text to be imported if it already exists on the image?

A few common real-world use cases where developers and site owners need to disable or intercept XMP alt text:

  1. Stock photos and photography software metadata: Adobe Lightroom, Photoshop, and stock libraries often embed internal photo IDs, camera tags, or copyright notices into the XMP metadata fields. These frequently do not accurately describe the image for screen reader users, so editorial teams and accessibility-focused sites often prefer enforcing manual alt text entry instead of auto-populating irrelevant metadata.
  2. External AI and DAM workflows: Sites that integrate with Digital Asset Management (DAM) platforms or automated vision services (e.g. AWS Rekognition, Cloudinary) to generate contextual alt text need a way to either bypass local file parsing or supply their own alt text directly via the pre_ filter without running redundant XML/DOM parsing.
  3. Core API consistency: Introducing pre_wp_get_image_alttext follows the standard Core short-circuit filter convention (like pre_get_document_title or pre_wp_unique_post_slug), ensuring full developer control and extensibility.

​@mppellicer commented on ​PR #14063:


6 weeks ago
#11

About tests:

  • Non-string returns (e.g. false, 0, an array) are not covered.
  • Consider using @dataProviders in tests to reduce duplication.
  • Also the DIR_TESTDATA . '/images/IPTC-PhotometadataRef-Std2025.1.jpg' is duplicated.

​@siliconforks commented on ​PR #14024:


6 weeks ago
#12

I'm going to close this PR (​PR #14024) in favor of ​PR #14063 because that one is the same thing except that it also contains tests. Any comments or reviews on the filter or the tests should be left over there. Thanks!

​@siliconforks commented on ​PR #14063:


6 weeks ago
#13

Thanks @mppellicer! The filter implementation from PR #14024 was included in this branch so that the unit tests could run and be verified in CI. If PR #14024 is merged first, I will gladly rebase this PR so it only contains the unit test file.

I've closed ​PR #14024 in favor of this one so that all discussion (for either the filter itself or the tests) will be in one place.

#14 in reply to: ↑ 6 @siliconforks
6 weeks ago

Replying to jorbin:

What are the reasons that you wouldn't want this alt text to be imported if it already exists on the image?

In addition to everything mentioned above in comment #10, I'd like to add a couple of points:

  1. The wp_get_image_alttext() function can be rather expensive since it reads the entire image looking for XMP metadata. For small images this is negligible, but for large images it might be up to a few tenths of a second. (Also for some reason wp_get_image_alttext() seems to be called twice, so whatever time it takes you end up paying the performance penalty twice.) This is still a fairly small amount of time - but why do this if you're not going to use the alt text anyway (or if you know that your images don't have any XMP metadata at all)?
  1. Another issue is that wp_get_image_alttext() parses XMP metadata with DOMDocument which, if I understand correctly, uses libxml2 to do the actual XML parsing. Unfortunately libxml2 has a fairly bad security track record and the current ​README file for libxml2 explicitly states: "It's NOT recommended to use this software to process untrusted data. There is a lot of ways that a malicious crafted xml could exploit a hidden vulnerability in the software." Now, this is probably not a problem if you're certain that all your images are coming from trusted sources, that your server's libxml2 library is completely up-to-date at all times, etc. However, if you're running a WordPress site with high security requirements I think it may be wise to simply turn wp_get_image_alttext() functionality off as a precaution.

#15 follow-up: @joedolson
6 weeks ago

I think it's fine to have a filter that is able to disable this, but I don't think it makes sense to just add a filter that explicitly targets *this* meta data path. I think it would make a lot more sense to have filters that cover all extracted meta data.

Something that set up a whitelist of approved meta filter elements, so that a developer could customize what gets extracted using a single filter.

#16 in reply to: ↑ 15 @siliconforks
6 weeks ago

Replying to joedolson:

I think it's fine to have a filter that is able to disable this, but I don't think it makes sense to just add a filter that explicitly targets *this* meta data path. I think it would make a lot more sense to have filters that cover all extracted meta data.

Something that set up a whitelist of approved meta filter elements, so that a developer could customize what gets extracted using a single filter.

That seems like a good idea. @therssoftware: do you want to try expanding ​PR #14063 to implement that?

#17 @therssoftware
5 weeks ago

Replying to joedolson and siliconforks:

I have updated ​PR #14063 to implement the unified allowed keys filter suggested by @joedolson.

Implementation Summary

  1. New Filter wp_read_image_metadata_allowed_keys: Added at the beginning of wp_read_image_metadata() in src/wp-admin/includes/image.php. Defaults to all supported keys: array( 'aperture', 'credit', 'camera', 'caption', 'created_timestamp', 'copyright', 'focal_length', 'iso', 'shutter_speed', 'title', 'orientation', 'keywords', 'alt' ).
  2. Targeted Bypass of Expensive I/O:
    • If 'alt' is omitted, wp_get_image_alttext() is completely bypassed, avoiding all XML/DOMDocument parsing and file read overhead.
    • If IPTC keys are omitted, iptcparse() is skipped.
    • If EXIF keys are omitted, exif_read_data() is skipped.
    • Returning array() from the filter skips all metadata extraction entirely.
  3. Strict Backward Compatibility: The returned $meta array maintains the complete 13-key schema with default fallback values ('', 0, array()), so calling functions, plugins, and templates will not encounter undefined array index notices.
  4. Retained pre_wp_get_image_alttext: Kept inside wp_get_image_alttext() for direct callers or external/AI alt text injection.

Test Coverage

  • Added dedicated unit tests in tests/phpunit/tests/image/readImageMetadataAllowedKeys.php (9 tests, 30 assertions).
  • Retained unit tests in tests/phpunit/tests/image/alttext.php (12 tests, 17 assertions).
  • Existing tests/phpunit/tests/image/meta.php (14 tests, 88 assertions) continues to pass 100% with zero regressions.
  • PHPCS passes cleanly with 0 errors and 0 warnings.

Props therssoftware, siliconforks, joedolson.


AI Disclosure: In accordance with the WordPress AI policy, I disclose that generative AI (Google Antigravity) was used in drafting and verifying these code changes and unit tests. All test executions and code reviews were performed locally in the development environment.

​siliconforks commented on ​PR #14063:


5 weeks ago
#18

Overall, the wp_read_image_metadata_allowed_keys filter seems fine to me. I'm not sure if we need the pre_wp_get_image_alttext filter any more, since you can accomplish the same thing with wp_read_image_metadata_allowed_keys?

​@therssoftware commented on ​PR #14063:


5 weeks ago
#19

Thanks for the thorough review and catch on the title and caption key coupling @siliconforks!

I have updated the PR accordingly:

  1. Title & Caption Decoupling in IPTC and EXIF:
    • Updated the IPTC 2#120 and EXIF UserComment logic so that allowing only title (without caption) successfully extracts the title while leaving $meta['caption'] empty.
    • Added dedicated unit tests covering independent title and caption extraction from both IPTC and EXIF in readImageMetadataAllowedKeys.php.
  1. Dropped pre_wp_get_image_alttext:
    • Removed the pre_wp_get_image_alttext filter from wp_get_image_alttext() in favor of the unified wp_read_image_metadata_allowed_keys filter as you suggested.
    • Omission of 'alt' from allowed_keys completely skips calling wp_get_image_alttext(), preventing all file read and XML parsing overhead.
    • Retained the core unit test suite for wp_get_image_alttext() in tests/phpunit/tests/image/alttext.php.

All tests pass cleanly in single site and multisite with 0 PHPCS violations. Ready for another look!

Note: See TracTickets for help on using tickets.