WordPress.org

Plugin Directory

Add Headers

Adds the ETag, Last-Modified, Expires and Cache-Control headers to HTTP responses generated by WordPress for more efficient caching.

Adds the ETag, Last-Modified, Expires and Cache-Control headers to HTTP responses generated by WordPress for more efficient caching.

Why is it useful?

Add-Headers gives you control over the ETag, Last-Modified, Expires and Cache-Control HTTP headers which your WordPress powered website sends back to the clients when dynamic pages are requested. This results in more efficient caching, reduction of bandwidth consumption and server load.

The aforementioned HTTP headers are sent to the client when visiting the page of a post, page, attachment, custom post type, the front page (static pages are supported) or a page of a category, tag, custom taxonomy, author or date based archive (pagination is supported). All feeds (posts feed, comments feed, post comments feed, archive feeds) are supported. Search results pages are supported, but no caching is permitted by default.

The value of each header is based on information provided by the post object in each of the above pages.

In case of archives, the first post is used in order to determine the time the archive was last modified. It is possible to customize this by utilizing the addh_archive_post filter (see below). Special consideration has been taken so that ETags are unique, even if a post appears first in more than one archives.

In case of feeds, the ETag and the Last-Modified headers are generated by WordPress itself. Add-Headers adds the Expires and Cache-Control headers to the feeds.

By default, the expiration date of each page is set to one day (86400 seconds) after the time the client accessed the resource, but this is configurable (see configuration section).

In addition to the Cache-Control header, the plugin also sets the Pragma header to cache or no-cache according to the defined max-age of the Cache-Control header.

Free License and Donations

Add-Headers is released under the terms of the GNU General Public License version 3 and, therefore, is Free Software.

However, a significant amount of time and energy has been put into developing and testing this plugin, so, its production has not been free from cost. If you find this plugin useful and if it has helped your blog pages get cached more efficiently and has saved you bandwidth and CPU load, you can show your appreciation by making a small donation.

Donations in the following crypto currencies are also accepted and welcome. Send coins to the following addresses:

  • BitCoin (BTC): 1KkgpmaBKqQVk643VRhFRkL19Bbci4Mwn9
  • LiteCoin (LTC): LS8UF39LfLahzGo49y736ooRYBVT1zZ2Fa

Thank you in advance for donating!

Configuration

The plugin can be configured by filtering the options array. You can add a filtering function like the following in the functions.php file of your theme:

function addh_custom_options ( $options ) {
    // These are the default options.
    return array_merge( $options, array(
        'add_etag_header' => true,
        'generate_weak_etag' => false,
        'add_last_modified_header' => true,
        'add_expires_header' => true,
        'add_cache_control_header' => true,
        'cache_max_age_seconds' => 86400,
        'cache_max_age_seconds_for_search_results' => 0,
        'cache_max_age_seconds_for_authenticated_users' => 0,
    ) );
}
add_filter( 'addh_options', 'addh_custom_options', 10, 1 );

The settings above demonstrate the default values.

By setting any of the max-age settings to 0, a Cache-Control: no-cache, must-revalidate, max-age=0 header is sent. cache_max_age_seconds_for_search_results only affects search results. cache_max_age_seconds affects all pages. If you need more customization, it is possible to filter the headers before they are sent to the client (see Available Filters section).

The rest of the options should be self-explanatory.

Available Filters

The following filters can be used to further customize the HTTP headers.

addh_options
  • applied to the plugin options array. The hooked function should accept and return 1 argument: an array of options. See the example in the configuration section above.
addh_cache_control_header_format
  • applied to the template that is used to generate the Cache-Control header's value. The hooked function should accept and return 1 argument: a string. The default template is "public, max-age=%s". The template must have one %s placeholder for the max-age seconds.
addh_headers
  • applied to all HTTP headers that have been generated by Add-Headers before they are sent to the client. The hooked function should accept and return 1 argument: an array of HTTP headers (strings).
addh_headers_feed
  • applied to all HTTP headers that have been generated by Add-Headers for a feed. The hooked function should accept and return 1 argument: an array of HTTP headers (strings).
addh_archive_post
  • applied to the post object that is used in order to generate HTTP headers for the whole archive. The hooked function should accept and return 1 argument: a post object.
addh_supported_post_types_singular
  • applied to the array of post types that are supported on pages where is_singular() is true. By default, regular posts, pages, attachments and public custom post types are supported. The hooked function should accept and return 1 argument: an array of post types.
addh_supported_post_types_archive
  • applied to the array of post types that are supported on pages where is_archive() is true. By default, regular posts and public custom post types are supported. The hooked function should accept and return 1 argument: an array of post types.

Testing

You can use curl in order to check the headers.

For example:

curl -I -L http://example.org/sample-page/
curl -I -L -H "Accept-Encoding: gzip,deflate" http://example.org/sample-page/
curl -I -L -H "If-Modified-Since: Mon, 16 Dec 2013 10:10:00 GMT" http://example.org/sample-page/
curl -I -L -H "If-None-Match: \"abcdefghijklmn\"" http://example.org/sample-page/

Development

The development takes place at:

https://bitbucket.org/gnotaras/wordpress-add-headers

Mirror:

https://github.com/gnotaras/wordpress-add-headers

Pull requests are welcome.

If you use Varnish

If you use Varnish, try the WPVM plugin to purge pages either automatically as they are updated or on demand.

Requires: 3.1.0 or higher
Compatible up to: 4.2.2
Last Updated: 2015-1-8
Active Installs: 1,000+

Ratings

4.3 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1 100,1,1
100,1,1 100,1,1
100,1,1
100,2,2
100,1,1 100,2,2 100,1,1 100,1,1 100,1,1
100,2,2
100,2,2