I need to conditionally show some content on a WordPress page based on session count. Basically, only show content to the visitor for his or her first 3 sessions. I don't have an analytics tag on the page so I started looking into implementing PHP session with session_start but I don't understand the conditional below as session_id is always empty in my var_dump, when I reload the page. I was going to use the conditional to generate a cookie with session count but the first var_dump always returns empty. Any ideas?
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
var_dump(session_id()); //always empty
if(!session_id()) {
session_start();
var_dump(session_id()); //never empty
}
}
function myEndSession() {
session_destroy ();
}