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?

1 Answer
1

For that I would suggest to use

'init'

hook.

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

Leave a Reply

Your email address will not be published. Required fields are marked *