Skip to main content
Tweeted twitter.com/StackWordPress/status/838805297221283840
Added 'headers' tag + rewritten the question based on the meaning & further comment
Source Link

Use send_headers How can I change HTTP headers only onto posts of a singlespecific category from a plugin

I have a plugin to inject httpfor 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' );

}

NowIt sets the above HTTP headers to all the posts. However, I want to only inject the headers ononly to the pages inof a specific category X. For example, say the category slug is x and id is 75.

I've wrappedTo set the function inHTTP headers specific to the posts of category if ( 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 wrapping it into 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.

Any ideas? How can I restrictAs the headerscategory check didn't work with the send_headers action hook, I've tried to onlydo it with wp_headers filter hook, like the category I want?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?

Use send_headers only on a single category

I have a plugin to inject http headers. It works fine.

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');

}

Now, I want to only inject the headers on pages in category X.

I've wrapped the function in if ( in_category('x') ) {} but that didn't work.

I've also tried wrapping it in this:

$cat = get_query_var('cat');
if ( $cat == '75' ) {}

and

global $post;
if ( in_category ( 'x', $post->ID ) ) {}

but those fail too.

Any ideas? How can I restrict the headers to only the category I want?

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

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?
Source Link

Use send_headers only on a single category

I have a plugin to inject http headers. It works fine.

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');

}

Now, I want to only inject the headers on pages in category X.

I've wrapped the function in if ( in_category('x') ) {} but that didn't work.

I've also tried wrapping it in this:

$cat = get_query_var('cat');
if ( $cat == '75' ) {}

and

global $post;
if ( in_category ( 'x', $post->ID ) ) {}

but those fail too.

Any ideas? How can I restrict the headers to only the category I want?