I need to show a different header file ("header-tourist.php" and "header-agent-php") based on a choice the visitor will make. For example, I have 2 main buttons: View site as Tourist or View site as Agent. Based on this, I would like to show a different header throughout the whole site.

What is the best way to approach this (global variables, sessions etc.) ?

  • You should post the code you have tried already, working or not. If not, we can't help you, instead we have to write all the code you need, which is equivalent to make the job for you. At least show us your effort to solve the problem. – cybmeta May 26 '14 at 15:53
  • I would set a cookie. This way if a user bookmarks a page for later reference it will retain his selection and display the appropriate header. – Matt Royal May 26 '14 at 16:08
  • I'm just asking what's the best way to approach this task, no code needed :) at all. Normally in PHP I would use $_SESSION but I don't know if this is recommended in WP or if there is a better approach to this. I can also create a function in function.php and work from there using globals – gabitech May 26 '14 at 16:29
  • @MattRoyal - Thanks for your input! it seems like cookies is the best way to go, at least with the little I know about the WP system – gabitech May 26 '14 at 17:32

I would approach this by setting sessions.

So when the user makes a selection set a session which contains the users choice.

Then in wp_head write an if statement that pulls the relevant template dependant upon the value of the session.

  • that's what I ended up doing - I created a simple "switcher" that based on session value sets the correct template. Thanks! – gabitech May 27 '14 at 13:42

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.