0

I am trying to write a plugin which logs in a file information about each request made by users. I hooked it to the init action, but i don't know how to get the response status for the request. All the other information like ip, user-agent is available on the $_SERVER variable, but this one is not. Any suggestions?

Corina
  • 15
  • 7
  • The final HTTP status code is likely not known at this point ... in the _init_ phase you can hardly expect it to be already determined whether that requested URL actually leads to any content, or would for example result in a 404 ... – CBroe Nov 08 '17 at 08:11
  • @CBroe so I need to hook it to another action? I've tried to hook it as a filter on http_response, but it doesn't seem to be called at each request and response... – Corina Nov 08 '17 at 08:19
  • Either find a hook that executes only after the response status code has been determined, or maybe look into a solution like this one, which relies on a basic PHP feature rather than WordPress, https://stackoverflow.com/questions/16120482/wordpress-run-an-action-after-page-sent – CBroe Nov 08 '17 at 08:32

1 Answers1

0

$_SERVER arrat doesn't contain current response status. So, try http_response_code() function instead.

Elvin Haci
  • 3,503
  • 1
  • 17
  • 22
  • I've already tried, but it shows always 200... i think that it shows the default response code, not the one generated by wordpress. – Corina Nov 08 '17 at 08:17
  • Is that different status shown in Chrome Network Inspector? – Elvin Haci Nov 08 '17 at 08:58
  • yes, if I access something that doesn't exist, in Chrome Network Insperctor it comes a 404 whereas with http_response_code() comes 200. – Corina Nov 08 '17 at 11:06
  • My bad, the function was working but the problem was that i called it too early, before the response status was known :D – Corina Nov 09 '17 at 15:48