WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 5 months ago

Last modified 4 months ago

#26623 closed enhancement (wontfix)

Make gallery_shortcode more customizable

Reported by: bradvin Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8
Component: Gallery Keywords: has-patch needs-refresh
Focuses: Cc:

Description (last modified by jeremyfelt)

I want to add a few simple changes to the gallery_shortcode function, which will allow for awesome customization possibilities if you are a theme or plugin author.

In summary I want to check for a class attribute in the shortcode, and add 4 filters to the function.

Details about the patch including a demo plugin which uses the resulting code can be found at https://github.com/bradvin/wordpress-gallery-interceptor

Here are a list of the changes in detail:

Class Shortcode Attribute

Check for a class attribute in the gallery shortcode. The class (if exists) is appended to the gallery container element.

Example:

[gallery class="foobar"]

Results in:

<div id="gallery-1" class="gallery galleryid-4 gallery-columns-2 gallery-size-thumbnail foobar">

Introduce a new filter gallery_class which can be used by themes or plugins to alter the gallery class. I realise that you could just use the class shortcode attribute above, but this allows for more customization.

Parameters:

  • $class - the current gallery class
  • $selector - the unique id of the gallery
  • $attr - all the gallery shortcode attributes, to allow for maximum customization

An example:

<?php
//add a class to the gallery depending on a template tag

function gallery_intercept_class( $class, $selector, $attr ) {
        if ( is_page() ) {
                return $class . ' page-gallery';
        }
        return $class;
}
add_filter( 'gallery_class', 'gallery_intercept_class', 10, 3 );

Introduce a new filter gallery_container_start which can be used by themes or plugins to alter the gallery container's opening HTML markup. You could change the tag or include any other markup you may want to introduce.

Paramaters:

  • $html - the current gallery opening markup (<div id='$selector' class='$gallery_class'>)
  • $selector - the unique id of the gallery
  • $gallery_class - the current gallery class
  • $attr - all the gallery shortcode attributes, to allow for maximum customization

An Example:

<?php
///change the tag to rather be a ul

function gallery_intercept_container_start( $html, $selector, $gallery_class, $attr ) {
        return "<ul id='$selector' data-class='$gallery_class'>";
}
add_filter( 'gallery_container_start', 'gallery_intercept_container_start', 10, 4 );

Introduce a new filter gallery_container_end which, as the name suggests, can be used to alter the closing markup of the gallery.

Paramters:

  • $html - the current gallery closing markup (<br style='clear: both;' /></div>\n)
  • $selector - the unique id of the gallery
  • $attr - all the gallery shortcode attributes, to allow for maximum customization

An Example:

<?php
///change the closing tag to a ul

function gallery_intercept_container_end( $html, $selector, $attr ) {
        return "</ul> <!-- $selector gallery -->";
}
add_filter( 'gallery_container_end', 'gallery_intercept_container_end', 10, 3 );

Introduce a new filter gallery_column_separator which can be used to alter the separator markup of the gallery. This filter can be used to override the <br /> tag that is inserted when the column count is met.

Paramters:

  • $html - the current gallery closing markup (<br style="clear: both" />)
  • $selector - the unique id of the gallery
  • $attr - all the gallery shortcode attributes, to allow for maximum customization

An Example:

<?php
//do not output any separator

function gallery_intercept_separator( $html, $selector, $attr ) {
        return "<!-- no separator please -->";
}
add_filter( 'gallery_column_separator', 'gallery_intercept_separator', 10, 3 );

Attachments (2)

media.php.patch (2.2 KB) - added by bradvin 2 years ago.
media(2).php.patch (2.5 KB) - added by bcworkz 2 years ago.

Download all attachments as: .zip

Change History (15)

@bradvin
2 years ago

#1 @DrewAPicture
2 years ago

  • Description modified (diff)
  • Keywords has-patch added

#2 @DrewAPicture
2 years ago

  • Description modified (diff)

#3 follow-up: @DrewAPicture
2 years ago

Once you reach a certain point, replacing the gallery shortcode with your own flavor altogether may be the better option.

Sounds like plugin material to me.

#4 in reply to: ↑ 3 @bradvin
2 years ago

Replying to DrewAPicture:

Once you reach a certain point, replacing the gallery shortcode with your own flavor altogether may be the better option.

Sounds like plugin material to me.

You may be correct in some instances, when it comes to extreme customization. But in most scenarios, for example, when I just want to remove the line breaks from the gallery HTML, why should I have to override the whole function, and then maintain my own version of it inside my theme or plugin? It just seems unnecessary, when a few filters will give me complete control over the HTML.

The new filters mean I can leave the gallery logic alone and only worry about the HTML.

#5 @bradvin
2 years ago

I made a copy-paste error in my description. The last filter name should be gallery_column_separator not gallery_intercept_separator

Is there a way I can edit the ticket?

#6 follow-up: @jeremyfelt
2 years ago

  • Description modified (diff)

Updating filter name to gallery_column_separator per @bradvin

#7 in reply to: ↑ 6 @bradvin
2 years ago

Replying to jeremyfelt:

Updating filter name to gallery_column_separator per @bradvin

thanks for that :)

#8 follow-up: @bcworkz
2 years ago

  • Cc bcworkz added

All those handy filters, and none met my need! I've offered a patch that includes my need to filter the $image_output within the loop.

I'm afraid this idea, while attractive, is going to eventually end up filtering every other line by the time we address everyone's need. We might be better off with a grand all encompassing filter for the final output. Certainly less than ideal, but just about everyone can make it work for them.

My particular need inside the loop was to inject a simple javascript call as the link target. As it stands now, to do this, I have to completely replace the entire thing and maintain my own version just to inject a little bit of script. That is certainly less than ideal as well.

#9 @SergeyBiryukov
2 years ago

  • Version changed from trunk to 3.8

#10 @chriscct7
7 months ago

  • Keywords needs-refresh dev-feedback close added

I agree with drew's concern in comment:4. There's already a way to style them, using the gallery class.

#11 @swissspidy
5 months ago

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

#12 in reply to: ↑ 8 @SergeyBiryukov
5 months ago

Replying to bcworkz:

We might be better off with a grand all encompassing filter for the final output.

Related: #14130

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


4 months ago

Note: See TracTickets for help on using tickets.