• Resolved adrian7

    (@adrian7)


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter adrian7

    (@adrian7)

    ***edit***
    I also need the current post ID (if is_single) to be available for my function.

    Thread Starter adrian7

    (@adrian7)

    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
    
           }
       }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to send a cookie to the browser.’ is closed to new replies.