WordPress.org

Make WordPress Core

Opened 14 months ago

Closed 11 months ago

Last modified 10 months ago

#50801 closed enhancement (fixed)

A filter for wp_get_attachment_image() html output

Reported by: prionkor Owned by: SergeyBiryukov
Milestone: 5.6 Priority: normal
Severity: normal Version: 5.4.2
Component: Media Keywords: has-patch has-unit-tests commit has-dev-note
Focuses: Cc:

Description

The function wp_get_attachment_image() is used to output image tags in WP sites. Currently it has a filter for attr but not filter on html output of the function. I am proposing a filter so plugin developers can modify its output.

An example use case:

Output of <picture> tag instead of <img> for supporting multiple format of images (jpeg, webp).

<picture>
   <source type="image/jpeg" src="foo.jpg" />
   <img src="foo.webp" alt="Bar" />
</picture>

Attachments (2)

50801.diff (2.6 KB) - added by audrasjb 12 months ago.
patch refreshed against trunk
50801.2.diff (2.2 KB) - added by antpb 11 months ago.
50801 - 2

Download all attachments as: .zip

Change History (27)

This ticket was mentioned in PR #440 on WordPress/wordpress-develop by prionkor.


14 months ago

  • Keywords has-patch added

Add wp_get_attachment_image filter in output of wp_get_attachment_image() Trac ticket https://core.trac.wordpress.org/ticket/50801

Trac ticket:

#3 @prionkor
14 months ago

  • Keywords dev-feedback added

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


14 months ago

#5 @SergeyBiryukov
14 months ago

  • Milestone changed from Awaiting Review to 5.6

This ticket was mentioned in PR #447 on WordPress/wordpress-develop by donmhico.


14 months ago

  • Keywords has-unit-tests added

Return a filter in wp_get_attachment_image(). Props to @prionkor for https://github.com/WordPress/wordpress-develop/pull/440. I initialized $attachment, add unit test, and filter docs.

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

#7 @prbot
14 months ago

prionkor commented on PR #447:

@donmhico Thank you! Appreciate it!

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


14 months ago

#9 @SergeyBiryukov
14 months ago

  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

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


12 months ago

#11 @audrasjb
12 months ago

  • Keywords needs-refresh needs-dev-note added

@audrasjb
12 months ago

patch refreshed against trunk

#12 @audrasjb
12 months ago

  • Keywords needs-refresh removed

#13 @Mista-Flo
12 months ago

Patch looks good, unit tests are passing, thanks for the refresh @audrasjb

This ticket was mentioned in Slack in #core-media by antpb. View the logs.


12 months ago

#15 @antpb
12 months ago

  • Keywords commit added; dev-feedback removed

In the recent Media component meeting all agreed this is commit candidate. Marking commit so we don't forget. :)

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


11 months ago

#17 @antpb
11 months ago

  • Keywords commit removed

I made some changes to the patch that I'd like to get some outside looks at. I've been talking this through with @garrett-eclipse and we landed on the below proposed patch. I've removed the @since in the parent function's doc block because we are covering the documentation in the new filter.

I've also matched the passed attributes for the parent function in the new filter so folks can override any of them. Happy to hear any feedback on that.

I'm removing commit only because I don't want it to be committed before we all agree this approach is best. I'm also blocked at the moment by some local tests failing due to an unrelated change here: https://core.trac.wordpress.org/ticket/49558#comment:12

Last edited 11 months ago by antpb (previous) (diff)

@antpb
11 months ago

50801 - 2

#18 @Mista-Flo
11 months ago

Patch looks good to me, slightly better hook

#19 @audrasjb
11 months ago

This new approach looks good to me as well, thanks @antpb 👍

#20 @hellofromTonya
11 months ago

  • Keywords commit added

The new approach looks good. My only suggestion is to remove the added $attachment = '' as it's no longer used outside of the if block or by the new filter.

<?php
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
        $attachment = '';

But this can be done when committing this patch.

This ticket was mentioned in Slack in #core-media by hellofromtonya. View the logs.


11 months ago

#22 @SergeyBiryukov
11 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 49234:

Media: Introduce a filter for wp_get_attachment_image() HTML output.

Props prionkor, antpb, donmhico, audrasjb, Mista-Flo, hellofromTonya.
Fixes #50801.

#25 @daisyo
10 months ago

  • Keywords has-dev-note added; needs-dev-note removed
Note: See TracTickets for help on using tickets.