I'm trying to set a cookie when a user is on a single post. I'm keeping track of how many single posts he has read this month. I have two problems:
I have a custom template and added the following code to my functions.php
function articlesVisited() {
if (is_single()){
$postNumber = $wp_query->post->ID;
setcookie($postNumber,'visited',time()+60*60*24*31);
}
}
add_action('init', 'articlesVisited');
First. The code inside is_single() is never reached when I visit a single post on my site. If I put an -else- block after it, it executes that.
Second, the $wp_query->post->ID does not give me the ID of the post. I think that has something to do with global variables...
Does anyone has experience with this?