I have written a plugin that sets cookies, and need to avoid the "Headers already sent" PHP error by placing the code at the very top before the head section. Themes usually place wp_head just before the close of the head section, so is there any efficient way to get a plugin to send this code without having to edit the theme itself?

  • Just to be clear: 'Headers already sent' refers to the http headers, not the <head> section of the html you generate. The http header contains information like the content-type of the body, and once you start emitting the body, you can't change the header anymore (as the header already has been sent to the client).
    – Femaref
    Commented Nov 23, 2014 at 14:56

1 Answer 1


For that I would suggest to use



According to codex

This runs after WordPress has finished loading but before any headers are sent

For more details visit codex explantion and examples for this hook;

Codex link.

You can find answer to similar question over here;

How to set custom cookies before output

  • 1
    Good to know that this worked for you. Just up vote and mark it as right answer so that it would help others as well. Commented Nov 23, 2014 at 12:44
  • @josh if this worked for you; can you mark it as right answer so that it could help others as well. Commented Aug 21, 2015 at 18:41

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.