WordPress.org

Ready to get started?Download WordPress

Plugin Directory

Add Headers

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

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

These 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, 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. 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).

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,
    ) );
}
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).

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: 3.8.3
Last Updated: 2014-2-8
Downloads: 1,282

Ratings

5 stars
5 out of 5 stars

Support

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

Got something to say? Need help?

Compatibility

+
=
Not enough data

2 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