Ask your WordPress questions! Pay money and get answers fast! (more info)

Small modification to plugin

  • SOLVED

Hello

I have this plugin that adds http headers:



<?php

add_action( 'send_headers', 'meadow_stop' );
function meadow_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');
}


This plugin works fine. But it adds the headers to all pages.

I want to change it so it only adds the headers on pages in category X.

So something like: if ( in_category('x') ) {}

But this doesn't work. Can you fix it?

Answers (7)

2017-03-05

Naeem Nur answers:

<?php
add_action( 'send_headers', 'meadow_stop' );
function meadow_stop() {
if ( in_category( 'X' ) ) {
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');
}
}


jimbob comments:

Sorry, but that does not work


Naeem Nur comments:

add_action( 'send_headers', 'meadow_stop' );
function meadow_stop() {
$cat = get_query_var('cat');
if ( $cat == '75' ) {
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');
}
}


try this, just replace '75' with category ID


jimbob comments:

No, this doesn't work either.

2017-03-05

Francisco Javier Carazo Gil answers:

Good evening,

The problem here is: the action is executed to soon so you have to work directly with the query.

So we have:


add_action( 'send_headers', 'meadow_stop', 10, 1 );
function meadow_stop( $object ) {
if( $object->query_vars['category_name'] != "category_X" )
return;


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


jimbob comments:

This is the closest answer so far.

But it adds the headers to the category archive page, not to the posts in that category.

I don't want the headers on the category archive page.
I only want to add the headers on posts in category X.


Francisco Javier Carazo Gil comments:

Ok, I understood bad.

So what you want is something like it:

if( !isset( $object->query_vars['p'] ) )
return;
$post_id = $object->query_vars['p'];
if( !in_category( $category, $post_id ) )
return;


jimbob comments:

I'm sorry, can you tell me the complete code?
I tried this (category name: catx) and it didn't work:

add_action( 'send_headers', 'meadow_stop', 10, 1 );
function meadow_stop( $object ) {

if( !isset( $object->query_vars['catx'] ) )
return;
$post_id = $object->query_vars['catx'];
if( !in_category( $category, $post_id ) )
return;


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


Francisco Javier Carazo Gil comments:

It cannot work because "catx" is not a query var :s

Sorry I though you knew a little more about it. p is correct, p is the ID.

Change $category var for the one you want to use: the category slug.

if( !isset( $object->query_vars['p'] ) )
return;
$post_id = $object->query_vars['p'];
if( !in_category( $category, $post_id ) )
return;


jimbob comments:

Hello,

Category name is quiz, so I used this code:
But it didn't work.

<?php
/*
Plugin Name: test
Plugin URI:
Description: test
Version: 1.0
Author:
Author URI:
License:
License URI:
*/

add_action( 'send_headers', 'meadow_stop', 10, 1 );
function meadow_stop( $object ) {

if( !isset( $object->query_vars['p'] ) )
return;
$post_id = $object->query_vars['p'];
if( !in_category( quiz, $post_id ) )
return;


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


jimbob comments:

tried this too:

<?php
/*
Plugin Name: test
Plugin URI:
Description: test
Version: 1.0
Author:
Author URI:
License:
License URI:
*/

add_action( 'send_headers', 'meadow_stop', 10, 1 );
function meadow_stop( $object ) {

if( !isset( $object->query_vars['p'] ) )
return;
$post_id = $object->query_vars['p'];
if( !in_category( 'quiz', $post_id ) )
return;


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


Francisco Javier Carazo Gil comments:

Maybe your permalink structure or the query vars is not working as we would like.

I would need to look into the system... but it would take more than this price.

2017-03-06

Arnav Joy answers:

try is_category('x') in place of in_category('x');

2017-03-05

jimbob answers:

.

2017-03-05

mod mi answers:

Hello, try this:

add_action( 'send_headers', 'meadow_stop' );
function meadow_stop() {
global $post;
if ( in_category ( 'x', $post->ID ) ) {
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');
}
}


jimbob comments:

Does not work

2017-03-05

Krishna Tiwari answers:

HI Jimbob,
Please try bellow code. Its worked.

add_action( 'send_headers', 'meadow_stop' );
function meadow_stop() {
$catid = 3; // Replace cat id according your requirement
if (check_category_exists( $catid )){
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');
}
}

function check_category_exists($catid){
$cat_to_check = get_term_by( 'id', $catid, 'category');
if ($cat_to_check){

return true;
}
else{return false;}
}


jimbob comments:

This adds the headers to every post and page, not only to posts under category id 3

2017-03-05

Rempty answers:

Hello jimbob
Do you want to add the headers on category pages or in posts with a category?

For send header in category pages
add_action( 'send_headers', 'meadow_stop' );
function meadow_stop() {
$category='1';
if(is_category($category)){
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');
}
}

If you want to add headers in posts with a category XYZ
add_action( 'send_headers', 'meadow_stop2' );
function meadow_stop2() {
global $post;
$category=1;
$taxonomy='category';
if(has_term($category,$taxonomy,$post)){
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');
}

}


jimbob comments:

Doesn't work


Krishna Tiwari comments:

Hi,
You want header only on post detail page (single.php), if given category belongs. Is right?


Rempty comments:

Hey jimbob
You can't use the send_headers because the query object was not created
instead use this code

add_action('template_redirect', 'add_last_modified_header');
function add_last_modified_header() {
if( is_singular() ) {
$category=7;
$taxonomy='category';
$post_id = get_queried_object_id();
if(has_term($category,$taxonomy,$post_id)){
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');
}
}
}