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.

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

  • add_filter should not be overwriting other calls to that filter, can you post your code for that?
    – mor7ifer
    Feb 2, 2012 at 13:17
  • I've added an example of the overwriting I mentioned in the post. Feb 2, 2012 at 18:14

1 Answer 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.

  • Ah ok. That's a pain. I'll have to recreate it then I guess, and make the changes in the copied original function. Lets hope the 'post_gallery_output' filter does get adopted soon :) - Thanks a lot for the info! Feb 3, 2012 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.