Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#52055 new enhancement

Allow modifying gallery_shortcode attributes before filtering the default HTML output.

Reported by: webbistro's profile webbistro Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords: has-patch dev-feedback
Focuses: Cc:


Hi there! I am an author of the Enhanced Media Library plugin, and I constantly encounter the same problem with enhancing media galleries/playlists.

The problem: I need to add a parameter to the gallery (shortcode). That parameter changes the query, and returns another set of attachment IDs (content of the gallery). I do not need to change the look (HTML) of the gallery, I just need to process gallery parameters and change its content which is supposed to be displayed by a theme or a plugin after my plugin changed the ids set.

So, I hook to the shortcode_atts.

But! In the gallery_shortcode function we have called the post_gallery filter, which allows totally redefine the gallery's output (HTML) and which is used by plugins and themes for this purpose.

If it was used, then the function returns its result and all code bellow is never executed.

So, the shortcode_atts function is never called in case that some theme/plugin changes just gallery HTML. We do not have any hook prior to the post_gallery to make changes to the gallery's content.

So, we have an option of whether to change the content OR the look of the gallery, which is, actually, weird.

Of course, I can use the post_gallery and change both the content and the look. But in this case, a theme or a plugin cannot change the look of my content after that.

I believe, that we have to separate modifying the gallery content query and the gallery output.

The simplest solution (I see) is to move

$output = apply_filters( 'post_gallery', '', $attr, $instance );

if ( ! empty( $output ) ) {
    return $output;

after the call of shortcode_atts.

But I also would like to separate the code of the gallery_shortcode into two parts:
(1) querying for the content with its hooks
(2) displaying HTML with its hooks.

Any thoughts on this?


Attachments (2)

52055.1.patch (2.3 KB) - added by Mista-Flo 3 years ago.
Create a new filter to edit shortcode attrs, and move the other filter a bit later
52055.2.patch (2.3 KB) - added by Mista-Flo 3 years ago.
Fix first patch

Download all attachments as: .zip

Change History (5)

#1 @hellofromTonya
3 years ago

  • Summary changed from Enhancing media gallery on the front-end is almost impossible. Improvement suggestion. to Allow modifying gallery_shortcode attributes before filtering the default HTML output.
  • Version changed from 5.6 to 2.5

Hello @webbistro,

Welcome to WordPress Trac! Thanks for your enhancement ticket.

I'm changing the ticket's version to 2.5 to match the version of the gallery_shortcode function. I also modified the ticket's title to make it easier for searching and quick understanding of the proposal.

#2 @Mista-Flo
3 years ago

  • Keywords has-patch needs-testing added

Hi Nadia, thanks for your ticket.

Indeed the gallery shortcode is not easy to deal with.

I guess TinyMCE editor is deprecated and the use of block editor is more encouraged, so it might not be a priority to enhance the shortcode.

But if we want to do so, I would suggest moving post_gallery after shortcode_atts like you said, and also add an other hook to only filter shortcode attributes if we want to. That way, if you don't want to change the HTM output, but only the query, you can do so. And you can also let other plugins interact with the output with your updated query.

Can you have some testing with this patch please?

3 years ago

Create a new filter to edit shortcode attrs, and move the other filter a bit later

3 years ago

Fix first patch

#3 @Mista-Flo
3 years ago

  • Keywords dev-feedback added; needs-testing removed

Actually the issue with this patch, is that if you actually filter the $atts var, if other plugins edit output with the other filter, they will still rely on $attr which does not contain edit parameter query.

But if I change the first parameter of the old filter to $atts, it will create a breaking change which is not good I think.

Any other thought?

Note: See TracTickets for help on using tickets.