0

I am building now a Wordpress site that has a automatic opened popup on the first page which gives the users the possibility to choose between 2 options (ex if they are private customers or company customers). Depending on selected option, I need to:

  1. show some pages to private customers
  2. show other pages to company customers
  3. show some pages to all customers (ex contact)

Any idea how can be this achieved? How can I flag the option and how to use this "flag" to filter the pages?

Thank you very much for your help!

Later edit: The specs demand that the option the user makes should be saved for a longer time, so the user will only get this popup only the first time he/she visits the site (and when the cookie - if that's the best solution - expires).

  • in WordPress you have a role system. you create a role "private_customer" and a role "compagny_customer". and then you just have to test in which role is the connected user to display or not a page codex.wordpress.org/Roles_and_Capabilities – mmm Mar 16 '16 at 14:11
  • Thank you for your really fast answer! I was considering a similar solution, but I wonder if I can establish a role for a user (more correct it is visitor) if the user does not use a login. So how can I associate a random user to a role only by allowing him to press a button, without making him login into the wordpress or use social login or anything. He should be kind of anonymous, but with a role. Is this possible? – adrian.jao Mar 16 '16 at 14:15
  • I want to be crystal clear: the specs of the project specify that the visitors of the site should be able to use it without needing any form of login. I think that this excludes your solution, mmm, but I might be wrong. – adrian.jao Mar 16 '16 at 14:20
  • if a unconnected visitor can choose what he want to see, you can put his choice in the PHP session ($_SESSION) and then read the value of this choice in all pages – mmm Mar 16 '16 at 14:42
  • It seems like a very good answer! I'll check it in detail and come with feedback later, but it seems it's exactly what I need. Thank you very much for your fast answers! – adrian.jao Mar 16 '16 at 14:49
0

Some commenters mentioned using $_SESSION to solve the issue, and to save a variable there (i.e. "visitor_type") to use it in showing pages.

This would work, but it's often discouraged to use $_SESSION in Wordpress, reasons can be read here.

However, I've also been in a situation where you can't avoid using $_SESSION.

What I would do, and please correct me someone if this is not a good solution, is to have 3 different menus, and by default use the menu that is shown to all visitors. Once the visitor clicks "Company" or "Individual", the page just refreshes with a $_GET variable set to "Company" or "Individual".

In the code where you call the menu, you can check $_GET for that value and load a different menu. A really simple solution could look like this:

if(isset($_SESSION["visitor_type"])){
wp_nav_menu( array( 'theme_location' => 'primary'.$_SESSION["visitor_type"] );
}
elseif(isset($_GET["visitor_type"]){
$_SESSION["visitor_type"] = $_GET["visitor_type"];
wp_nav_menu( array( 'theme_location' => 'primary'.$_GET["visitor_type"] );
}
else{
wp_nav_menu( array( 'theme_location' => 'primary_generic' );
}

Now it's up to you to intelligently handle $_SESSION complications, like if you want to allow visitors to switch type etc...

Good luck.

Edit: As I was writing this, someone mentioned Cookies. Some reading on them is available here.

  • Thank you very much for your input. I probably did not explain the scenario very good in the beginning. I will update my original post to make clear that I want to save the option for a longer time (the user will not change his/her status), and considering this I think that cookies are a better option than sessions (also there are some security flaws for sessions). – adrian.jao Mar 16 '16 at 15:44
  • PHP sessions use cookie so it's like the same – mmm Mar 16 '16 at 16:49

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.