WORDPRESS HTTP 304 ETAG HEADER PLUGIN

By deploying some additional code to your WordPress site, it can cut down on the need to have Google etc have to download every page on your site. The idea of HTTP 304 is to tell a search engine a given page is unchanged. By using a hash value, a tag called ETAG can be used to show a page has changed quickly.

This class simply sends HTTP 304 unchanged or the contents if modified. The design was to minimize server bandwidth as some ISPs have caps.

This plugin acts as a cache for WordPress so remove other caching plugins which defeat the purposes of this one which is designed for a stock install of WordPress.

/*
Plugin Name: WordPress HTTP 304 Etag Manager 
Plugin URI: https://linux-guru.azurewebsites.net/wordpress-http-304-etag-header-plugin/
Description: Etags reduce server bandwidth by using HTTP 304 unchanged
Version: 20170305
Author: Linux Guru
Author URI: https://linux-guru.azurewebsites.net
License: GPL 3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: wporg
Domain Path: /
*/

/*
WordPress HTTP 304 Etag Manager is free software: you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.

WordPress HTTP 304 Etag Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of'
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {URI to Plugin License}.
*/

class etag_cache_headers_plugin { 
    private function remove_html_comments($content = '') { 
        return preg_replace('/<!--(.|\s)*?-->/', '', $content); 
    } 
    public function start_buffer(){ 
        global $wp_customize; 
        if ( !isset( $wp_customize ) and ! is_admin()) { 
            ob_start(); 
            add_action('shutdown', array($this,"print_buffer"), 0); 
        } 
    } 
    public function print_buffer() { 
       $output = ob_get_contents(); 
       ob_end_clean(); 
       $output = $this->remove_html_comments($output); 
       $output = apply_filters( 'etag_cache_headers_filter', $output); 
       $etag = wp_hash($output); // call the WP hash 
       //set Google/Bing etag-header 
       header( "Etag: ".$etag ); 
       global $wpdb; 
       $wp_last_modified_date = $wpdb->get_var("SELECT GREATEST(post_modified_gmt, post_date_gmt) d
           FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY d DESC LIMIT 1");
       $wp_last_modified_date = max($wp_last_modified_date, get_lastcommentmodified('GMT')); 
       $last_modified = mysql2date('D, d M Y H:i:s', $wp_last_modified_date, 0) . ' GMT'; //set last-modified header 
       header( "Last-Modified: ".$last_modified); //check expiration 
       header("Expires: ".$last_modified); 
       if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified || @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
           header("Cache-Control: no-cache, must-revalidate"); 
           header("Pragma: no-cache"); 
           header("HTTP/1.1 304 Not Modified"); 
           exit; 
       } else { 
           echo $output; 
           die; 
       } 
   }
   public function __construct() { 
       add_action('template_redirect', array($this,"start_buffer")); 
   } 
} 
$etag_cache_headers_instance = new etag_cache_headers_plugin();

If you use this code in a plug-in, be sure to wrap it with PHP tags as WP expects HTML in the editor. Generally this generic class is typical for any wordpress plugin. The PHP class is the best mechanism for large scale projects.