WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 4 years ago

Last modified 3 years ago

#29832 closed enhancement (wontfix)

Add filter to final output of gallery_shortcode

Reported by: dcrabill Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.5
Component: Gallery Keywords:
Focuses: template Cc:

Description (last modified by SergeyBiryukov)

This is similar to #26623 but a lot simpler.

The attached patch adds a "gallery_content" filter to the end of the gallery shortcode.

It would be nice if the gallery shortcode had an available filter at the end, before the shortcode output was returned. This would be similar to the "the_content" filter, but just for a gallery.

I am currently trying to use a lightbox plugin on a gallery, but it needs to insert an extra attribute onto the image links. It currently utilizes the "the_content" filter and works well on galleries added to the main content of a post.

However, I am trying run the gallery shortcode outside the main content, so the plugin doesn't work. As far as I can tell, the plugin author doesn't have a way to get his lightbox to work on galleries added outside the main content or excerpt.

I can understand why the "post_gallery" filter is called so soon, for devs who want to completely replace the functionality of this shortcode. I don't want to replace it, nor do I want the lightbox plugin to replace it... I want to use the standard Wordpress gallery, but with that particular lightbox functionality added to it. This patch would allow that.

Attachments (1)

media.php.patch (476 bytes) - added by dcrabill 5 years ago.
Adds a gallery_content filter at the end of gallery_shortcode

Download all attachments as: .zip

Change History (8)

@dcrabill
5 years ago

Adds a gallery_content filter at the end of gallery_shortcode

#1 follow-up: @SergeyBiryukov
5 years ago

  • Description modified (diff)

Related: #14130, #20230.

I am currently trying to use a lightbox plugin on a gallery, but it needs to insert an extra attribute onto the image links.

gallery_shortcode() calls wp_get_attachment_link(), so you can use the wp_get_attachment_link filter.

I can understand why the "post_gallery" filter is called so soon, for devs who want to completely replace the functionality of this shortcode.

You can actually change the output of the shortcode using that filter without completely replacing the functionality:

function filter_gallery_shortcode_output_29832( $gallery, $attr ) {
	remove_filter( 'post_gallery', __FUNCTION__, 10, 2 );

	$gallery = gallery_shortcode( $attr );

	add_filter( 'post_gallery', __FUNCTION__, 10, 2 );

	// Change the output
	// $gallery = ...

	return $gallery;
}
add_filter( 'post_gallery', 'filter_gallery_shortcode_output_29832', 10, 2 );

#2 @dcrabill
5 years ago

Thanks Sergey. I had noticed those related tickets but their patches didn't look like they were doing the same thing as this.

Those solutions will get things working for me for now. The wp_get_attachment_link filter does work and is certainly better than nothing, though this then applies the lightbox to all attachments, not just galleries.

That code to change the output is an interesting solution. I hadn't thought of that and it is clever, though it seems kind of hacky. Do you know why the decision was made to stick a filter at the beginning of the shortcode without one at the end? I would have thought that the end would be the first place a filter would go, but then again, I didn't write it. Sorry for my ignorance about code design.

Perhaps, considering the number of related tickets, it's worth considering whether there is value in having a filter for adjusting the final output of the gallery. Isn't that the way many other elements of core work?

#3 @boonebgorges
5 years ago

  • Version changed from trunk to 2.5

#4 @caitrionad
4 years ago

+1 for being able to modify the innards of gallery_shortcode - I frequently find myself wanting to add a class to a gallery item while continuing to use the rest of the standard implementation.

The solutions above were helpful in achieving that but I'd love if there were a cleaner way.

BTW, re. using wp_get_attachment_link to modify a gallery item's link, there is a way to apply that solely to gallery attachment links:

add_filter(
    'post_gallery',
    function(){
        add_filter('wp_get_attachment_link','custom_modify_gallery_shortcode_link');
    }
);

(found here: http://wordpress.stackexchange.com/questions/134811/add-gallery-id-to-rel-attribe-of-wp-get-attachment-link)

#5 @DrewAPicture
4 years ago

  • Keywords close added

For what it's worth, I also wrote an answer awhile back on StackExchange for adding a data attribute to gallery image elements. That was applicable to integration with the Galleria script and uses the wp_get_attachment_image_attributes filter.

http://wordpress.stackexchange.com/a/160555/33309

I would reiterate that the gallery shortcode is already littered with hooks and adding more is really just throwing gas on the fire. It's the jalopy of shortcodes at this point.

Version 0, edited 4 years ago by DrewAPicture (next)

#6 @obenland
4 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I agree. Parsing html output to replace or add something is really not ideal. The the_content filter comes with its own set of problems and is to me an argument against adding a filter like that.

#7 in reply to: ↑ 1 @gnotaras
3 years ago

Replying to SergeyBiryukov:

function filter_gallery_shortcode_output_29832( $gallery, $attr ) {
	remove_filter( 'post_gallery', __FUNCTION__, 10, 2 );

	$gallery = gallery_shortcode( $attr );

	add_filter( 'post_gallery', __FUNCTION__, 10, 2 );

	// Change the output
	// $gallery = ...

	return $gallery;
}
add_filter( 'post_gallery', 'filter_gallery_shortcode_output_29832', 10, 2 );

@SergeyBiryukov that was a nice workaround for #36270 !

Note: See TracTickets for help on using tickets.