Modifying HTTP response header in your WordPress site

Though Google recommends responsive web design most, they also support sites which produce different HTML on the same URL depending on user-agent information. My site is the latter. Google’s recommendation to sites like mine is to use ‘Vary’ HTTP header.

But with standard installation, WordPress doesn’t set ‘Vary’ HTTP field in HTTP response header. So I need to insert it in the header by modifying functions.php.

To insert HTTP header fields, write a filter function for ‘wp_headers’ in your functions.php.

function add_vary_header($headers) {
    $headers['Vary'] = 'User-Agent';
    return $headers;
}

add_filter('wp_headers', 'add_vary_header');

You can also remove unwanted header fields by unsetting them. (related information)

function remove_x_pingback($headers) {
    unset($headers['X-Pingback']);
    return $headers;
}

add_filter('wp_headers', 'remove_x_pingback');

These changes are applied to your WordPress site except admin pages.

9 thoughts on “Modifying HTTP response header in your WordPress site”

  1. Thanks, this was very helpful for an issue I had. Used the folllowing in my case:


    function add_http_header($headers) {
    $headers['X-UA-Compatible'] = 'IE=edge,chrome=1';
    return $headers;
    }

    add_filter('wp_headers', 'add_http_header');

Comments are closed.