18

Is there a dedicated WP function, action or filter to use when adding/modifying the HTTP headers?

For now I just hook a PHP header() call into the WP 'init' hook like this:

add_action('init', 'add_header_xua');
function add_header_xua(){
    if(!is_admin()){
        header('X-UA-Compatible: IE=edge,chrome=1');    
    }
}

But is this the correct way to do that?

4 Answers 4

9

The init action is the wrong place to do it. A better place would be at template_redirect, so that you only affect the front end view of the site and not the admin areas.

2
  • good point regarding the hook, but apart from that, is calling the 'raw' header() function (using the right hook) the correct way, or is there a wp wrapper that should be used for this? Calling header directly only allows for adding to the headers, not modifying them, like it would be possible with a filter (like when you want to manipulate the body class) Commented Jun 16, 2011 at 16:04
  • 1
    header() is the only way. However, it does have a second parameter that will let you replace existing headers instead of adding to them. See php.net/manual/en/function.header.php
    – Otto
    Commented Jun 17, 2011 at 18:49
15

Here is the code I've used, based on the original question and on Dominic P's answer...

/*
 * Modify HTTP header
 */
function add_header_xua($headers) {

    // var_dump($headers); #=> if you want to see the current headers...  

    if (!is_admin()) {
        $headers['X-UA-Compatible'] = 'IE=edge,chrome=1';    
    }

    return $headers;     
}
add_filter('wp_headers', 'add_header_xua');

Once you've added that code to your functions.php file, you can check it works by running a test at http://web-sniffer.net/ to ensure the HTTP headers have indeed changed.

0
11

I know it's been a while, but if anyone else stumbles on this, I found a WordPress hook specifically for modifying HTTP headers. The hook is wp_headers and it's called in the wp class.

The first argument passed is an array of headers with the header name as the key. The second argument is a reference to the wp class object.

11
  • There is another hook: send_headers. I do not see any "wp_headers" on the hook list.
    – pixeline
    Commented Nov 1, 2014 at 18:26
  • Hmm, that's interesting. You checkout the wp_headers filter here.
    – Dominic P
    Commented Nov 1, 2014 at 18:39
  • I think (but am not sure) that there are "internal" hooks (such as wp_headers), and "public" hooks (such as "send_headers"). "internal" hooks are more prone to be changed in future releases. "public" hooks are deemed more "stable". But I may just be wrong here, I don't find any source to back up this feeling I got from various researches I did on hooks.
    – pixeline
    Commented Nov 1, 2014 at 18:43
  • That is very possible, but in this case it looks like the two hooks simply perform different functions. send_headers is an action that fires after the headers are sent while wp_headers is a filter on the headers that is applied before they are sent.
    – Dominic P
    Commented Nov 2, 2014 at 18:22
  • well...if the fact that only send_headers has a codex page is any indication of which to use...
    – drzaus
    Commented Dec 11, 2015 at 20:14
4

send_headers is the preferred method over wp_headers for this situation as demonstrated in the codex.

is_admin() || add_action('send_headers', function(){ 
    header('X-UA-Compatible: IE=edge,chrome=1'); 
}, 1);

Here's my explanation for why on a similar question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.