Make WordPress Core

Opened 9 years ago

Closed 6 years ago

#33979 closed enhancement (maybelater)

Add filter for 'post_gallery_item'

Reported by: lostmotionassembly's profile lostmotionassembly Owned by: antpb's profile antpb
Milestone: Priority: normal
Severity: normal Version: 4.4
Component: Gallery Keywords: has-patch
Focuses: Cc:

Description

This would allow developers to customise the individual gallery items, without having to duplicate the entire gallery_shortcode() function to do so. This would make it quicker, easier to understand, and reduce the likelihood of the gallery shortcode functionality 'drifting' away from core. A sample use case would be wanting to add additional classes to the gallery item based on attachment meta (such as additional orientation options like 'square' or 'panorama').

Attachments (4)

33979.diff (894 bytes) - added by mauteri 9 years ago.
Added filter for gallery item classes.
33979-2.diff (908 bytes) - added by antpb 7 years ago.
33979-3.diff (906 bytes) - added by antpb 7 years ago.
33979.4.diff (1.7 KB) - added by pbiron 6 years ago.
pass an array, rather than a string, to filter. and sanitize the return value of the filter.

Download all attachments as: .zip

Change History (21)

#1 @wonderboymusic
9 years ago

  • Keywords needs-patch added

@mauteri
9 years ago

Added filter for gallery item classes.

#2 @mauteri
9 years ago

  • Keywords has-patch added; needs-patch removed

#3 @antpb
7 years ago

  • Milestone changed from Awaiting Review to 4.9.3

I've refreshed the patch for 4.9.3. This could be very useful going forward.

@antpb
7 years ago

#4 @SergeyBiryukov
7 years ago

  • Milestone changed from 4.9.3 to 5.0

New hooks are generally added in major releases, moving to 5.0.

#5 @antpb
7 years ago

No prob! I've attached a new patch with the proper version in the docs.

@antpb
7 years ago

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


7 years ago

#8 in reply to: ↑ 6 @desrosj
7 years ago

Replying to tristangemus:

Similar? https://core.trac.wordpress.org/attachment/ticket/38228/patch_38228.3.diff

This is similar but a different context. That ticket aims to filter the entire generated output of the shortcode whereas this one is being used for just a single image within the gallery. Love both of these tickets, but we should definitely keep them separate :)

I would also like to see the ability to modify more than just classes using this filter. Adding data attributes to the tag, or changing the image size for the current image would be great. That could allow more creative gallery layouts, such as the tiled gallery in Jetpack. https://jetpackme.files.wordpress.com/2013/11/example-gallery-tiled.jpg

Version 0, edited 7 years ago by desrosj (next)

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


7 years ago

#10 @antpb
7 years ago

  • Owner set to antpb
  • Status changed from new to assigned

It was mentioned by @mikeschroder in the Media Component meeting today that we should look into how this may impact Gutenberg. My gut tells me that this wont enhance the Gutenberg experience since the filter is set somewhere in the shortcode functions but maybe we can look into this through the Gutenberg lens.

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


6 years ago

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


6 years ago

@pbiron
6 years ago

pass an array, rather than a string, to filter. and sanitize the return value of the filter.

#14 @pbiron
6 years ago

we discussed the possibility of including this in 4.9.8 during today's bug scrub. It was decided not to do so, because it was felt the latest patch (at that time) was missing a few things.

33979.4.diff addresses those things:

  1. filter should accept an array (instead of string) for $classes; this is for consistency with similar existing filters, e.g., post_class;
  2. filter return value cast to array() (just in case);
  3. sanitize_html_class() applied to each element in the filter return value array;

Notice that 33979.4.diff begins by passing an empty array to the filter, and then adds gallery-item in as the final step before the item's opening tag is added to the output. I'd be OK doing it the way the previous patch did (i.e., passing array( 'gallery-item' ) to the filter, but it seems safer to me to do it the way I did.

#15 @pento
6 years ago

  • Milestone changed from 5.0 to 5.1

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


6 years ago

#17 @desrosj
6 years ago

  • Milestone 5.1 deleted
  • Resolution set to maybelater
  • Status changed from assigned to closed

This one needs to be re-evaluated. A lot has changed in the last 6 months, and the path forward is the gallery block (and blocks in general), not the gallery shortcode (and shortcodes in general).

Not against adding a filter for the gallery shortcode, but there are a lot more factors to consider now that the block editor has been released. If someone wants to dig back into this, it can be reopened and explored further.

Note: See TracTickets for help on using tickets.