Function Inside Conditional Tag

Say I have a function like this:

add_filter("post_gallery", "fix_my_gallery_wpse43558",10,2);
function fix_my_gallery_wpse43558($output, $attr) {

    // blah, blah, blah

}

Basically, the function above allows me to override the built-in (AKA default) WordPress image gallery template using the post_gallery filter.

Read More

The thing is, I would like to override the default WordPress image gallery template ONLY IN my custom feed, for which I need to use the if ( is_feed( $feeds = 'custom_feed' ) ) { .... } conditional tag.

The question is, what is the right way to operate the function inside the conditional tag?

if ( is_feed( $feeds = 'custom_feed' ) ) {

    add_filter("post_gallery", "fix_my_gallery_wpse43558",10,2);
    function fix_my_gallery_wpse43558($output, $attr) {

        // blah, blah, blah

    }

}

or

add_filter("post_gallery", "fix_my_gallery_wpse43558",10,2);
function fix_my_gallery_wpse43558($output, $attr) {

    if ( is_feed( $feeds = 'custom_feed' ) ) {

        // blah, blah, blah

    }

}

Related posts

Leave a Reply

2 comments

  1. I would go with your first solution, as the filter-function only needs to get executed when you are inside your feed.

    But there is no “right way” as both solutions should work…

  2. I would use the first one. It’ll be faster as the filter will be ignored when not required.

    Also, in the second example, you would also need to throw back out the $output, which again, is a bit wasteful from a speed perspective.