admin管理员组

文章数量:1122832

I'm trying to change the cache-control header of a specific post (1234). I tried adding the following to the end of my functions.php:

add_filter('wp_headers', 'wp_test_headers');
function wp_test_headers($headers)
{
    if ( is_single ( 1234) ) {
       $headers['Cache-Control']="no-store, no-cache, must-revalidate, max-age=0";
    }
    return $headers;
}

However when I open the page of the post in my browser, the condition is never met. Should I be adding the filter elsewhere, or is there another issue?

I'm trying to change the cache-control header of a specific post (1234). I tried adding the following to the end of my functions.php:

add_filter('wp_headers', 'wp_test_headers');
function wp_test_headers($headers)
{
    if ( is_single ( 1234) ) {
       $headers['Cache-Control']="no-store, no-cache, must-revalidate, max-age=0";
    }
    return $headers;
}

However when I open the page of the post in my browser, the condition is never met. Should I be adding the filter elsewhere, or is there another issue?

Share Improve this question edited Jan 6, 2018 at 22:31 simplicity asked Jan 6, 2018 at 21:57 simplicitysimplicity 12 bronze badges 2
  • 1 if it's the page post type, you need is_page. – Milo Commented Jan 6, 2018 at 22:05
  • Is there such thing as a default post type? I created it by going to the posts sidebar and adding a new post. – simplicity Commented Jan 6, 2018 at 22:07
Add a comment  | 

1 Answer 1

Reset to default 0

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

add_action( 'template_redirect', 'update_header_cache' );
function update_header_cache() {
    if( is_single( 1234) ) {
        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');
    }
}

本文标签: filtersModify wp headers on specific page