Make WordPress Core

Opened 5 years ago

Closed 5 months ago

Last modified 6 weeks ago

#52055 closed enhancement (worksforme)

Allow modifying gallery_shortcode attributes before filtering the default HTML output.

Reported by: webbistro's profile webbistro Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.5
Component: Media Keywords:
Focuses: Cc:

Description

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

<?php
$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?

Best,
-Nadia

Attachments (2)

52055.1.patch (2.3 KB) - added by Mista-Flo 5 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 5 years ago.
Fix first patch

Download all attachments as: .zip

Change History (8)

#1 @hellofromTonya
5 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
5 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?

@Mista-Flo
5 years ago

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

@Mista-Flo
5 years ago

Fix first patch

#3 @Mista-Flo
5 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?

#4 @callumbw95
6 months ago

  • Keywords close added

Hi all,

I have just taken a look at this, and as mentioned by @Mista-Flo 4 years ago shortcodes and tinymce are not being actively worked anymore, and I believe the official status for these components is that they are code complete, meaning they will only be receiving bug / security patches going forwards. Thank you to everyone who was involved with this ticket. 😃

#5 @mindctrl
5 months ago

  • Keywords has-patch dev-feedback close removed
  • Resolution set to worksforme
  • Status changed from new to closed

Today it's possible to filter the gallery shortcode attributes via the shortcode_atts_gallery filter. Example:

add_filter( 'shortcode_atts_gallery', function( $out, $pairs, $atts, $shortcode ) {
    $out['include'] = '27'; // change IDs provided in shortcode
    return $out;
}, 10, 4 );

Source: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/shortcodes.php#L676-L690

Also as Callum noted, shortcodes are on the list of components being proposed for a form of "maintenance mode", where no new features are added. Source: https://make.wordpress.org/core/2025/07/02/proposal-introduce-maintenance-mode-for-components/

I'm going to close this ticket to help clean up Trac, but feel free to reopen if you think otherwise.

#6 @swissspidy
6 weeks ago

  • Milestone Awaiting Review deleted

Removing milestone from closed ticket.

Note: See TracTickets for help on using tickets.