Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#8480 closed defect (bug) (invalid)

apply_filters('post_gallery', '', $attr) should be apply_filters('post_gallery', $attr);

Reported by: chionsas.org Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.7
Component: Gallery Keywords: gallery, shortcode, apply_filters
Focuses: Cc:


When customizing the gallery shortcode functionality via 'post_gallery' filter, the filter function does not receive [gallery] shorttag inline arguments (eg. [shorttag columns="5"])

This seems to be bacause of bogus parameters, sent to apply_filters() funcion on http://trac.wordpress.org/browser/trunk/wp-includes/media.php#L601

Correct syntax, according to http://codex.wordpress.org/Function_Reference/apply_filters is:
apply_filters($tag, $value);

When that 601 line in wp-includes/media.php is changed to:
$output = apply_filters('post_gallery', $attr);
(that is, the second argument is removed), everything works as expected.

It seems that the bug was made because of add_filter() funcion syntax that takes three parameters - tag, priority and argument count. This would explain the empty second argument.

I don't consider this bug to be urgent, but as the fix is extremely simple, it could surely make the 2.7 final :)
This affects 2.7RC1, 2.6x and (I beleive) all the way down to when gallery functionality was introduced.

Change History (7)

#1 @DD32
9 years ago

  • Milestone 2.7 deleted
  • Resolution set to invalid
  • Status changed from new to closed

The filter is correct.

Apply_filters() can accept multiple params, You need to use add filters like this:

add_filters('post_gallery', 'my_gallery', 10, 2);

(10 = default priority, 2 = accept 2 parameters)

the function should be as such:

function my_gallery($gallery_html, $attrs) {
return $gallery_html

#2 @DD32
9 years ago

Oh, Theres a possibility you were mis-understanding the use of that filter now i look at it again.

the post_gallery filter is to completely override the builtin gallery function, Its not used to modify gallery arguements. In the event that a non-empty string is returned from the filter, the function uses that string as the gallery content instead of generating its own.

#3 @chionsas.org
9 years ago

Ok, accepting two parameters is a good workaround (should have thought of that one myself), but I still don't understand the need for the first parameter (since it's always an empty string).. :?

I understand the purpose and use of post_gallery filter (I'm using it to completely replace the gallery_shorttag() function with one of my own), but still don't get why there are TWO arguments passed, when even the original gallery_shorttag() function takes in only one (an array of inline attributes).

#4 follow-up: @DD32
9 years ago

but I still don't understand the need for the first parameter (since it's always an empty string).. :?

Think of it like this, The empty string IS the gallery content.

If $attr was passed in as the first param, then it would be filtering the $attr array, Which could give the wrong impression. $attr is mearly extra information, its not whats being filtered.

#5 @DD32
9 years ago

It also allows for multiple filters, eg:

add_filter('post_content', 'f1', 10, 2);
add_filter('post_content', 'f2', 10, 2);

function f1($content, $attr) { return 'F1' . $content };
function f2($content, $attr) { return 'F2' . $content };

That would result in 'F1F2' as the gallery, If only $attr was passed, It'd be impossible to have multiple filters on it (Which while you may not see the need, There are occasions which it could be useful).

#6 @DD32
9 years ago

Er.. replace "post_content" with "post_gallery" in that last comment..

#7 in reply to: ↑ 4 @chionsas.org
9 years ago

Think of it like this, The empty string IS the gallery content.

Oh, ok. I get it now :)

thanx for your patience and sorry for the invalid bug (though it could help someone by turning out in a search)

Note: See TracTickets for help on using tickets.