Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
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) – mwb Jun 16 '11 at 16:04
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 Jun 17 '11 at 18:49

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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