This is general question but lets say I have a function and on a specific page I want to display a table.

The function only applies to one page on my site but every post/page will run this function (and skip it when it ‘realises’ it isn’t the right page).

Would it be better, from a performance point of view, to use a page template for this specific page instead of this filter? Or is this negligible and not worth worrying over.

Hopefully you get the gist of what I’m asking, I realise there are
other factors like how my table (or whatever I’m including) is
processed but lets ignore that for the sake of this question.

add_filter('the_content', 'str_load_table');
function str_load_table($content) {
    if(is_page('my-table')) {
        $my_content="pretend this is a table";
        $content = $content . $my_content
    }
    return $content;
}

2 Answers
2

The difference would be negligible.

The the_content filter is fired regardless of whether you use it or not because WordPress uses it for other things. So the only thing that would be performance related would be the complexity of your filter function.

But since you’d probably be doing the same level of complexity in a template or in the filter function, there’s no real difference between the two.

I would say that you’d be better using the filter if you want the table to be part of the main content area. Then you don’t have to worry as much about how/what styles are applied.

Leave a Reply

Your email address will not be published. Required fields are marked *