I have a plugin for injecting additional HTTP headers. It works fine with the following CODE:
add_action( 'send_headers', 'jim_stop' );
function jim_stop() {
header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' );
header( 'Pragma: no-cache' );
header( 'Expires: Thu, 01 Dec 1994 16:00:00 GMT' );
}
It sets the above HTTP headers to all the posts. However, I want to inject the headers only to the pages of a specific category. For example, say the category slug is x
and id is 75
.
To set the HTTP headers specific to the posts of category x
, I've used in_category( 'x' )
in conditional as follows:
add_action( 'send_headers', 'jim_stop' );
function jim_stop() {
if ( in_category( 'x' ) ) {
// category specific HTTP headers
}
}
but that didn't work.
I've also tried to check the category like this:
$cat = get_query_var( 'cat' );
if ( $cat == '75' ) {
// category specific HTTP headers
}
and also like this:
global $post;
if ( in_category ( 'x', $post->ID ) ) {
// category specific HTTP headers
}
but those fail too.
As the category check didn't work with the send_headers
action hook, I've tried to do it with wp_headers
filter hook, like the following:
add_filter( 'wp_headers', 'add_header_xua' );
function add_header_xua( $headers ) {
if ( in_category( 'x' ) ) {
// category specific HTTP header changes
}
return $headers;
}
but it fails too. So my question is:
- How can I restrict the HTTP header changes only to the posts of a specific category?
function add_header_xua($headers) { // var_dump($headers); #=> if you want to see the current headers... if ( in_category('x') ) { $headers['X-UA-Compatible'] = 'IE=edge,chrome=1'; } return $headers; } add_filter('wp_headers', 'add_header_xua');