adrian7
Member
Posted 7 months ago #
Ok, so I have the code :
function set_my_cookie(){
setcookie('my_cookie', '2011', 8600, '/', get_bloginfo('url')); //set the cookie
}
This is attached as a hook to wp_head add_action('wp_head', 'set_my_cookie'); .
It doesn't works since wp_head() is called after some output is sent to the bowser and I am supposed to set the cookie BEFORE any output is sent.
So what hook can I use to make it work?
adrian7
Member
Posted 7 months ago #
***edit***
I also need the current post ID (if is_single) to be available for my function.
adrian7
Member
Posted 7 months ago #
Good news I finally figured a way out. Heredown the code:
add_action('posts_results', 'set_my_cookie');
function set_my_cookie(){
global $wp_query;
$cpost = $wp_query->posts[0]; //get the current post
if(!wp_is_post_revision($cpost)) {
if( is_single() || is_page() ) {
$id = $cpost->ID;
setcookie('my_cookie_for_'.$id, '2011', 8600, '/', get_bloginfo('url')); //set the cookie
}
}
}