If Modified Since Header in WordPress

I am trying to enable 304 If Modified Since HTTP header in my WordPress site. After doing lot of Googling I find out a site where the author said to put the following line at the very end of wordpress wp-config.php file. Here is the line of code:

header("Last-Modified: " . the_modified_date());

Now the author said that this was it. I dont have to do anything else to achieve 304 If Modified Since HTTP header. But after doing this I tested by HTTP header using the site http://httpstatus.io/ and here is the screenshot of my header:

Read More

enter image description here
(check the red marked section). The last modified header value is BLANK.

After that I thought this might be some issue with the_modified_date() function so I’ve also tried get_the_modified_date() function. But still no result.

At the very end, I’ve created a small shortcode function to test if these functions are working or not and echoed it inside the short code. When I used the shortcode I can clearly see that the functions are working fine but for some reason sending blank to the 304 If Modified Since HTTP header.

Please guys, help me to fix this issue. I’m out of clue and no idea how to achieve this.

P.S.: My site is www.isaumya.com

Related posts

Leave a Reply

1 comment

  1. the_modified_date() is a template tag that must used inside the loop, that is why it is not wokring for you.

    WordPress provide a action and filter hook to include or modify HTTP headers:

    But it doesn’t work for this purpose. For example, the next code is not working:

    add_action( 'send_headers', 'cyb_add_last_modified_header' );
    function cyb_add_last_modified_header() {
        //Check if we are in a single post of any type (archive pages has not modified date)
        if( is_singular() ) {
            $post_id = get_queried_object_id();
            if( $post_id ) {
                header("Last-Modified: " . get_the_modified_time("D, d M Y H:i:s", $post_id) );
            }
        }
    }
    

    Why?

    The main wp query is not build at this moment, neither in wp_headers filter. So, is_singular() returns false, get_queried_object_id() returns NULL and there is no way to get the modified time of the current post.

    A posible solution is to use template_redirect action hook, as suggested by Otto in this question (tested and working):

    add_action('template_redirect', 'cyb_add_last_modified_header');
    function cyb_add_last_modified_header($headers) {
    
        //Check if we are in a single post of any type (archive pages has not modified date)
        if( is_singular() ) {
            $post_id = get_queried_object_id();
            if( $post_id ) {
                header("Last-Modified: " . get_the_modified_time("D, d M Y H:i:s", $post_id) );
            }
        }
    
    }
    

    Please Note

    The question is answered by @cybmeta at here. I’m just sharing the answer here so that if anyone is looking for the answer here, he/she will find it. All credit goes to @cybmeta.