• In a normal page, we can rewrite the header like this:

    
    add_filter('send_headers',function(){
        header('Vary: User-Agent');
    });
    
    
    add_filter( 'wp_headers', function( $headers ) {
            $headers['Vary'] = 'User-Agent';
            return $headers;
    });
    

    But this rewrite doesn’t work on pages with page caching by “fastest cache plugin ” working.
    If we disable the plugin or disable the page cache, we can rewrite the header normally.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Emre Vona

    (@emrevona)

    this is an expected result. when a page is cached, the page is saved as a static html file so PHP and Mysql do not work.

    Thread Starter realmccoyzz

    (@realmccoyzz)

    @emrevona

    I have the following in functions.php.

    
    add_filter('send_headers',function(){
        header('Vary: User-Agent');
    });
    

    I expected the header information to be output dynamically as it is not page cached.
    Does add_filter not work if static html file is responded?

    Plugin Author Emre Vona

    (@emrevona)

    yes, the add_filter does not work after static html file is created.

    you need to do it with Ajax request.

    Thread Starter realmccoyzz

    (@realmccoyzz)

    @emrevona

    For example, Certainly the following is not reflected while the static html file is being returned.

    
    add_filter('the_content', function(){
        return 'Hello!!';
    });
    

    However, if we regenerate the page cache, it will be reflected.

    Regarding “header information”, since it is not included in the page cache, it will not be reflected even if the static html is regenerated.
    My understanding is that this is a sequence issue.

    Plugin Author Emre Vona

    (@emrevona)

    you mean that the code works before creating cache so the “Hello!!” keyword must exist in the html source. right?

    Thread Starter realmccoyzz

    (@realmccoyzz)

    @emrevona

    the code works before creating cache so the “Hello!!” keyword must exist in the html source

    yes, of course.

    
    add_filter('the_content', function(){
      return 'Hello!!';
    });
    

    Write the above in functions.php and regenerate the page cache, it will be reflected correctly.

    
    add_filter('send_headers',function(){
        header('Vary: User-Agent');
    });
    

    But in the above case it is not reflected.
    Simply put, send_headers is disabled by WP fastest cache plugin.

    Plugin Author Emre Vona

    (@emrevona)

    can you try another cache plugin please?

    Thread Starter realmccoyzz

    (@realmccoyzz)

    @emrevona

    If I want to add response headers to cached pages, how exactly do I do that?

    Plugin Author Emre Vona

    (@emrevona)

    I have no idea.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Cannot rewrite header in page cache’ is closed to new replies.