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..

How to conditionally load your scripts based on an ACF field

How to conditionally load your scripts based on an ACF field

This tutorial provides the PHP codes that can be pasted in order to enqueue your scripts conditionally based on the value of an ACF field…
Categories:
Pro
Check If The Latest Post Was Published in The Last X Minutes

Check If The Latest Post Was Published in The Last X Minutes

In this Pro tutorial we shall define a custom function that takes in the number of minutes as an argument and returns true if there…
Categories:
Create A Customizable AJAX Add To Cart Button In Bricks Builder

Create A Customizable AJAX Add To Cart Button In Bricks Builder

In Bricks, you can simply create an Add To Cart button from a dynamic data / function {woo_add_to_cart}. This button supports AJAX as well if…
Pro
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Weekday Condition in Bricks

Weekday Condition in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
Categories:
Checking if the current post has been published within the last x days

Checking if the current post has been published within the last x days

Conditionally output an element if the current post has been published in the last x number of days.
Categories:
Pro
Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Checking if the current Page/Post has Children i.e. is a Parent in Bricks

Shows how we can check whether the current Page or Post (of any hierarchical post type) is a parent
Categories:
Pro
Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

Checking ‘PublishPress Future’ plugin’s Future Action enabled posts

PublishPress Future is a handy plugin for scheduling actions like unpublishing or deleting posts (of any post type) at a specified date and time in…
Categories: