Make WordPress Core

Opened 4 years ago

Closed 3 years ago

Last modified 3 years ago

#50801 closed enhancement (fixed)

A filter for wp_get_attachment_image() html output

Reported by: prionkor's profile prionkor Owned by: sergeybiryukov's profile 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 3 years ago.
patch refreshed against trunk
50801.2.diff (2.2 KB) - added by antpb 3 years ago.
50801 - 2

Download all attachments as: .zip

Change History (27)

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


4 years ago
#1

  • 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
4 years ago

  • Keywords dev-feedback added

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


4 years ago

#5 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 5.6

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


4 years ago
#6

  • 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

prionkor commented on PR #447:


4 years ago
#7

@donmhico Thank you! Appreciate it!

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


4 years ago

#9 @SergeyBiryukov
4 years ago

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

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


3 years ago

#11 @audrasjb
3 years ago

  • Keywords needs-refresh needs-dev-note added

@audrasjb
3 years ago

patch refreshed against trunk

#12 @audrasjb
3 years ago

  • Keywords needs-refresh removed

#13 @Mista-Flo
3 years 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.


3 years ago

#15 @antpb
3 years 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.


3 years ago

#17 @antpb
3 years 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 3 years ago by antpb (previous) (diff)

@antpb
3 years ago

50801 - 2

#18 @Mista-Flo
3 years ago

Patch looks good to me, slightly better hook

#19 @audrasjb
3 years ago

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

#20 @hellofromTonya
3 years 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.


3 years ago

#22 @SergeyBiryukov
3 years 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
3 years ago

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