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.

Before reading any further

Before reading any further or trying this plugin on your WordPress platforms and, especially before adding a rating, please make sure you have thoroughly studied the FAQ. This plugin might not be what you think it is. Also, this plugin might not be the right plugin for you.

Why is this plugin useful?

Add-Headers gives you control over the ETag, Last-Modified, Expires and Cache-Control HTTP headers which your WordPress powered web site sends back to the clients when dynamic pages are requested. This gives you control over the way your dynamic content is cached by web browsers or caching proxy servers. If configured properly for your specific needs, it can result in more efficient caching of your content and thus reduce the bandwidth consumption and server load.

How it works

The aforementioned HTTP headers are sent to the client when visiting:

  • posts
  • pages
  • attachment pages
  • custom post types
  • the front page (static pages are supported)
  • archives (category, tag, custom taxonomy, author or date based archives, including paginated pages, are supported).
  • feeds, including posts feed, comments feed, post comments feed, archive feeds (all feed types are supported).
  • search results pages are supported (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 content pages (posts, pages, custom post types), the time the content has been last modified is determined by the time the content was last edited or by the creation time of the latest comment, whichever of the two is newer.

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 response.

By default, on search results and any page, that has been requested by an authenticated user, the max age of the response is set to 0, so as to prevent it from being cached.

By default, the max age and thus the expiration date of each page, which is supposed to be cached, 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 public 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 Bitcoin are also accepted and welcome. Send coins to the following addresses:

  • BitCoin (BTC): 1KkgpmaBKqQVk643VRhFRkL19Bbci4Mwn9

Thank you in advance for donating!

Configuration

This plugin does not have an administration interface so as to maintain the maximum level of flexibility when configuring it.

The plugin can be configured by attaching a filtering function to the addh_options hook which returns an array containing the options that should be used for the current request. The code can be added in the functions.php file of the theme or in another plugin:

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 to the client. 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.4
Last Updated: 2015-6-9
Active Installs: 2,000+

Ratings

3.9 out of 5 stars

Support

0 of 1 support threads in the last two months have been resolved.

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
100,1,1
100,1,1
100,1,1