15th Aug '24
/
4 comments

Conditional Output in Bricks based on Visitor Selection

Updated on 16 Aug 2024

A user asked:

How to dynamically show menu based on 'user type' selected

Hi, starting out on a project moving a clients site from hubspot to wordpress.

On their homepage, users select what type they are, eg. homeowner, architect etc. The site recognises the option chosen and shows a different menu option for each one.

Any ideas how I can change the ‘user type’ to whatever they have chosen so it shows the right menu - the menus will output conditionally, ie ‘ if the field = homeowner, only show x and y

Hope that makes sense.

Home to select option: https://cemfloor.co.uk

...when they navigate to other pages after choosing user type, the same menu button with the user type stays present based on their choice. Any ideas - I was trying to create a header that displays the button and menu conditionally based on their user type choice - trying to set a cookie on session storage using bricks interactions on the homepage and then output conditionally based on that.

Also the logo goes to the chosen user type ‘home’ page too once they have selected one.

This Pro tutorial shows how

  • localStorage can be used to store/persist the user type on the client-side and
  • cookies can be used to make the user type accessible to PHP on the server-side and
  • AJAX can be used to sync the user type with server

to transfer data between server and browser for effectively using dynamic conditions in Bricks to conditionally output elements depending on which button the visitor has clicked.

This is quite a powerful technique to have in your toolbox after you understand how it is put together and you should be able to extend it to build various solutions like age gate and custom cookie banners.

In this example we shall set up three buttons on the homepage each linking to corresponding pages:

and add three corresponding buttons (can be any other element) in the Header template:

Each button in the header has a dynamic data condition set on it.

These header buttons will not be visible on the homepage when a visitor visits the site for the first time or has not clicked on any of the homepage buttons yet.

When a visitor clicks the Homeowner button on the homepage for example, our custom JavaScript sets localStorage and a cookie (user_type = homeowner), then navigates to the corresponding /homeowners/ page.

In the dynamic condition on the Homeowner header button, we call a custom function that checks if the user_type cookie's value is homeowner and since that is true when the Homeowner homepage button is clicked, the header button will be shown.

The cookie's value will remain the same until another homepage button is clicked thus enabling us to output different elements based on which button the visitor clicked.

Update 1

Added instructions on how to add a select dropdown in addition to the buttons for the visitors to select their user type.

Step 1

Edit your site's homepage with Bricks and inside a Section's Container, paste this Block.

The three buttons (anchor tags to be precise) have user-type-link class.

They are each linked to /home-owners/, /contractors/, and /channel-partners/ respectively. While it does not matter, you may want to create these 3 Pages just so you don't keep coming across 404 Not Found pages during your testing after you implement the steps.

The buttons have IDs set at STYLE → CSS → CSS ID to homeowner, contractor and channel-partner respectively. The value of user_type cookie will be set to the ID of whichever button is clicked.

Step 2

Install and activate the Bricks child theme if it is not already active. You can download the Bricks child theme directly from your Bricks account.

This can be done at any stage, even if your site is already built with the parent Bricks theme, and will not have any negative impact.

Create a directory called assets in the child theme.

Create a directory called js inside assets.

Create a file named say, localstorage.js in the above js directory having:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…
Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Conditionally Outputting Elements only for Posts that have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories:
Pro
City-based and Country-based Conditional Output in Bricks

City-based and Country-based Conditional Output in Bricks

This Pro tutorial provides the steps to render a Bricks element only if the visitor is from the specified country or city. Note: Caching may…
Categories:
Pro
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…
Pro
Month and Day Condition in Bricks

Month and Day Condition in Bricks

This Pro tutorial shows how we can define a custom function that takes in a month in the three-letter format (like Dec) and a day…
Categories:
Pro
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
Pro
Condition for checking Bricks content

Condition for checking Bricks content

Only outputting Bricks content when a Page is built with Bricks, otherwise display WP content.
Categories: