Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
    
I'm creating a custom tracking plugin that uses the $post-ID value, and will be run on every public facing page. –  Dan Gayle Sep 24 '10 at 22:04
    
Unfortunately, $post->ID is not available until after the headers have been sent. Here's how the load sequence goes after WP loads: WP::parse_request() reads the request and turns it into variables for WordPress to use, then WP::send_headers() sends the header info to the browser (one of 404, html, or xml). Then WP_Query::parse_request() and WP_Query::get_posts() fire, setting up those globals. –  John P Bloch Sep 24 '10 at 23:26
1  
As long as I send the cookies before any html is sent, all will be well. So that would be where? template_redirect? I think that's the last action hook available before the page gets send across, correct? –  Dan Gayle Sep 27 '10 at 16:50
3  
I'd go with 'wp'. It fires immediately after the globals are registered; that's the absolute earliest you can use the $post global. –  John P Bloch Sep 27 '10 at 17:42
    
Adding an action to 'wp' works perfectly! –  Dan Gayle Sep 27 '10 at 20:44

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.

share|improve this answer
    
+1 for putting it in functions.php, if you can. Worked for me. –  Yarin May 10 '11 at 11:06

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.

if ( !defined( 'ABSPATH' ) ) { // we're being called directly, to check the cookie
     if ( file_exists('../../wp-config.php') )
                  include('../../wp-config.php');
     elseif ( file_exists('../../../wp-config.php') )
               include('../../../wp-config.php');
    else
       die('Could not find wp-config.php');

A better way might exist now. The plugin was written over a year ago.

share|improve this answer
1  
This is all being done from WITHIN WordPress, why would I need to check if WP is running? –  Dan Gayle Sep 27 '10 at 16:01
    
@Dan Gayle in your case you wouldn't. This plugin has to redirect to the verification page to set the cookie which is outside of WordPress. –  Chris_O Sep 27 '10 at 16:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.