0

I now usually php sessions are started from the init hook, but get_header runs before writing the page, sono technically speaking, I can open a session from there. Is there any other reason I should not do it?

add_action('get_header', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession(){
  if(!session_id()) {
    session_start();
  }
}

function myEndSession() {
  //session_destroy ();
}
  • Why though? What's wrong with init? – Jacob Peattie May 28 '18 at 15:23
3

There’s nothing that requires that get_header run before output starts, you could put it in the middle of a template file after other content and still have a perfectly valid theme.

template_redirect is the last action where it is guaranteed that no output has started yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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