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?
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;
You can find answer to similar question over here;