0

Basically, I am writing a wordpress plugin. The first function my plugin does is identify what HTTP response code and headers are scheduled to be sent (before sending them).

Based on the http status code, it would place a hook and may modify the HTTP response based on other things.

This is my first plugin I am writing and you could guess my experience in this - I am still with the basics of plugin writing.

3 Answers 3

2

see headers_list() and http_response_code().

Note that http_response_code() is new and might not be available in your PHP version. There is no other way to detect it. Sorry. But if you didn't change it yourself, the status code should be "200 OK", always.

/* get headers and HTTP status code (which should be always 200, 
   if you didn't change it before calling this code)  */

if (!headers_sent()) {
    $headers = headers_list();
    $status = http_response_code();
    // change if necessary
}
else {
    // oh noes, cannot change headers anymore
}
Sign up to request clarification or add additional context in comments.

Comments

2

The headers_list function will give you a list of all headers either sent or about to be sent. You can determine if they've already been sent with the headers_sent function, but I don't think you can guarantee to intercept them before they're sent. I could be wrong about that though - it might be possible with output buffering, but I've never looked into that.

1 Comment

Hi James! Thanks for replying. I just checked headers_list(). It returned the following: Array ( [0] => X-Powered-By: PHP/5.3.5 [1] => X-Pingback: mysite.com/xmlrpc.php [2] => Content-Type: text/html; charset=UTF-8 ) No HTTP status code. I need HTTP status code...
2

You have to use headers_sent() and headers_list() in combination, check if the headers are not sent with the first one and iterate on the results provided by the second one and change them according to your logic

1 Comment

Thanks for replying... You are right, but headers_list() doesn't give HTTP status code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.