A
A
Anton Seredny2017-03-18 21:01:40
Computer networks
Anton Seredny, 2017-03-18 21:01:40

Why is the Last-Modified header not being returned?

I need to add the title specified in the title, but it does not work. Here is the code I am using:

function add_header_xua($headers) {

  if( is_singular() ) {
        $post_id = get_queried_object_id();
        if( $post_id ) {
            $headers['Last-Modified'] = get_the_modified_time("D, d M Y H:i:s", $post_id);    // не появляется в заголовках
            $headers['Last-Modified2'] = get_the_modified_time("D, d M Y H:i:s", $post_id);  // появляется в заголовках  
        }
    }

    return $headers;     
}
add_filter('wp_headers', 'add_header_xua');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Seredny, 2017-03-18
@smidl

Understood and set up.
1) Changed PHP version from 5.4to 5.6
2) Changed filter wp_headersto action template_redirect. Since at the time wp_headers is triggered, the $post type variables have not yet been initialized, which means that all post type checks, getting its ID, etc. - impossible

function lastmode_headers() {

  if(is_singular()) {
    global $post;
    $LastModified = get_the_modified_time("D, d M Y H:i:s", $post->id);

    $IfModifiedSince = false;
    if (isset($_ENV['HTTP_IF_MODIFIED_SINCE']))
      $IfModifiedSince = strtotime(substr($_ENV['HTTP_IF_MODIFIED_SINCE'], 5));  
    if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
      $IfModifiedSince = strtotime(substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 5));
    if ($IfModifiedSince && $IfModifiedSince >= $LastModified_unix) {
      header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
      exit;
    }
    header('Last-Modified: '. $LastModified);  
  }
}
add_action('template_redirect', 'lastmode_headers');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question