How to filter ‘post_gallery’ after all other filters/plugins etc

I’m trying to re-format the gallery output a bit, change default columns and link setting, as well as exclude the featured image from the gallery images.

I can use *add_filter(‘post_gallery’ …)* in functions.php but that seems to overwrite any previous calls to this filter by the core and plugins.

Read More

Example of this overwriting as requested by m0r7if3r:

function test1($content, $attr) { return $content.'Test One ';}
add_filter('post_gallery', 'test1', 10, 2);
function test2($content, $attr) { return $content.'Test Two ';}
add_filter('post_gallery', 'test2', 10, 2);

Instead of outputting the original gallery and appending ‘Test One’ to the output, the first function there just outputs ‘Test One’ on it’s own. Interesting if I apply a second function as above, ‘Test One Test Two’ is outputted.. weird I think! Any idea why?

Alternatively I can *remove_shortcode(‘gallery’, ‘gallery_shortcode’)* and add my own, but then this also removes any manipulation by other scripts/plugins.

Note: there seems to be a future solution here where a *post_gallery_output* filter has been suggested, but no idea when this will be added to the core. I’m not familiar with how and when patches like the one suggested get added, so more info about that would also be helpful.

So… any ideas how I can achieve this now? Primarily my main issue is the removal of the featured image. I can filter the content to change the gallery column and link type for now.

Thanks! Ben

Related posts

Leave a Reply

1 comment

  1. Add filter is not intended to replace fully the existing code, but rather to append it. In the case of post_gallery, if you return ANYTHING but '', it will not do ANY of the default action. If you look on line 767 of /wp-includes/media.php, you can see where the hook is applied and work around that. Depending on how much you want to do, gallery_style can be used to just add CSS to the default…if you want to do more than that, you’ll have to write an entirely new output.