I know that a should set a cookie before any output.

I want to set a cookie with the actual page's taxonomy name. To get the taxonomy name I am using get_query_var('taxonomy).

The problem is:

If I hook the cookie creation into init the get_query_var returns empty. If I hook it too late I get a headers already sent error.

How can I accomplish this?

  • I do have a select field to sort page results (price asc/desc, title asc/desc). To reorder the items I need to re-run the query and for that I need to know the taxonomy name and terms . The request to reorder the items is sent via ajax... Apr 14, 2016 at 13:01
  • you could send that data with your ajax request rather than set a cookie.
    – Milo
    Apr 14, 2016 at 19:01
  • There is a case when I need to set the cookies inside an other AJAX request. That's why I need cookies here... Apr 15, 2016 at 0:53

1 Answer 1


Try hooking on the get_header action hook. That's the last action hook before headers are output.

  • template_redirect is technically the last action that is guaranteed to execute before output. whether or not get_header is called before output starts is theme-dependent and may not work consistently across all themes.
    – Milo
    Apr 15, 2016 at 4:22

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.