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 630 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Showing a Popup conditionally in Bricks based on Query Parameter

Showing a Popup conditionally in Bricks based on Query Parameter

Here's two ways a Bricks Popup can be shown on page load depending on URL parameter value.
Categories:
Tags:
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:
[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…
Pro
Post Content in Offcanvas using AJAX in Bricks

Post Content in Offcanvas using AJAX in Bricks

Updated on 14 Feb 2024 This Pro tutorial provides the steps to set up a list of posts whose titles which when clicked shows that…
Categories:
Pro
Conditional Output in Bricks based on if Content has Headings

Conditional Output in Bricks based on if Content has Headings

How to prevent the output of the table of contents if the content has no headings.
Categories:
Tags:
Pro
AJAX Pagination for Posts in Bricks

AJAX Pagination for Posts in Bricks

This Pro tutorial provides the steps to implement Loops & Logic for loading the next set of paginated posts when the pagination numbers are clicked…
Check if a given category has at least one post

Check if a given category has at least one post

There could be situations where you want to check if a given category (by its slug) has at least 1 post assigned to that category.…
Categories:
Pro
How to Insert Element(s) Between Query Loop Posts in Bricks

How to Insert Element(s) Between Query Loop Posts in Bricks

Update on 16 Aug 2023: Follow this tutorial instead. This Pro tutorial shows how we can insert a Div (or any custom HTML) after nth…