What’s a surefire way to check/set cookies before the php headers are sent? Is there an action or filter that would be the best place to hook a setcookie()
function?
Leave a Reply
You must be logged in to post a comment.
What’s a surefire way to check/set cookies before the php headers are sent? Is there an action or filter that would be the best place to hook a setcookie()
function?
You must be logged in to post a comment.
Depends on whether or not you need to check against WordPress’ user authentication. If you need to know whether they’re a logged in user, hook onto
'init'
. Otherwise, the sooner the better.If it’s something that should fire on every page load, and only checks for existence of the cookie and doesn’t need to tap into any of WP’s APIs, I’d put it into a custom MU-plugin named
0000a.php
to ensure that it fires before any non-core files could accidentally send headers.I think
functions.php
is definitely processed before any output and is fitting place for extensions.As for hook, maybe
after_setup_theme
, it comes right after that.You can create a function to check or set a cookie but you have to check for wp-config.php because if you use a redirect the file will get accessed directly before index.php on the redirect.
This is from a plugin that Mark Jaquith wrote Age Verification that requires the user to fill out an age verification form before entering the site. I modified it for a liquor, beer, wite site I did.
A better way might exist now. The plugin was written over a year ago.