How can I change HTTP headers only to posts of a specific category from a plugin

Why it doesn’t work:

send_headers hook is fired once the HTTP headers for caching, content etc. have been sent, but before the main query is properly initiated. That’s why in_category() function doesn’t work when you use it in this action hook. Same goes for the wp_headers filter hook.

To make it work:

  1. You may use the template_redirect action hook (or even wp action hook). This hook is fired after the main database query related objects are properly initiated, but before any output is generated. So this hook is good for setting additional HTTP headers, redirecting to another URL etc.

  2. Also you need to check is_single() as you want to apply it only to the posts from that category. Otherwise it’ll apply to the category archive page as well.

So the working CODE will be like the following:

add_action( 'template_redirect', 'jim_stop' );
function jim_stop() {
    if( is_single() && in_category( 'x' ) ) {
        header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
        header('Pragma: no-cache');
        header('Expires: Thu, 01 Dec 1990 16:00:00 GMT');
    }
}

Now the category check will work and those additional HTTP headers will only apply to the posts from category x.

Leave a Comment