In this tutorial we’ll look at how to we can make use of Zion Builder’s dynamic data settings to output different data in templates based on our own custom conditions. Specifically we’ll look at the ‘function return value’ option that allows us to use our own code.

Note – this is a more advanced tutorial than our other tutorials as it involves creating custom functions.

One of the main missing features of Zion Builder at the time of writing is to be able to display or hide elements based on different conditions. That feature appears to be on the roadmap and is tagged as being worked on currently, which is great.

Until then, if we’re building templates with the theme builder, there’s often going to be times where we need to switch out some text or a heading that will change depending on the current view, or perhaps we need to have a different button or link to direct a user to a different page.

How to make this change conditionally?

Using Dynamic Data / Function Return Value

One way is to take advantage of the ‘Function return value’ inside of the dynamic data options.

What this option does is allows us to populate the content with our own function. Whatever value the function returns, this is what will be displayed.

The freedom here means that we can include our own conditions inside of the function so the value returned will be different depending on the condition.

The benefit is doing this over using a code block (where we could also just write a condition to output what we need) is that the functions can be used globally across the site. We can create a function that includes a condition and then use that over and over for different content.

It also means better integration with elements inside Zion Builder, text, headings, buttons, links, images..

Example: Users who Has Purchased a Download

Let’s use Easy Digital Downloads as an example. They have their own shortcodes for displaying information to users, but let’s imagine we want to have a single button that either takes the user to their account page if they’ve purchased. Or it takes the user to a product page if they haven’t.

We need both the button text and the button URL to change depending on the purchase status.

Setting up our global function

The simplest way to add code snippets to sites built with Zion Builder would be a code snippets plugin. This can add code that is site wide. it has the same outcome than if you were adding code to a theme or a custom plugin, but can be done inside the dashboard.

We’ll add a custom function called ‘user_purchased_edd’ which will have a condition to find out if a particular download ID was purchased for the current user.

function user_purchased_edd( $download_id, $has_purchased_content = '', $hasnt_purchased_content = '' ) {
	
	$user_id = get_current_user_id();        // Get the user ID using the standard WP function
	$download_id = intval($value);   	 // Get the download ID, as an integer
	
	if ( function_exists('edd_has_user_purchased' ) ) {
		return ( edd_has_user_purchased($user_id, $download_id ) ) ? $has_purchased_content : $hasnt_purchased_content;
	} else {
		return '';
	}
 
}

The ‘edd_has_user_purchased’ function comes with Easy Digital Downloads and basically just returns true if the user has purchased the download, and returns false if they haven’t. So we can use this to output two different things depending on if this is true or false.

So if the user has purchased, then our function returns $has_purchased_content, (which will be whatever we add into our second argument in the function). If they haven’t purchased, then it returns $hasnt_purchased_content, (which will be whatever we add into the third argument).

Let’s look at how it can be then used..

How to use this in Zion Builder.

Let’s say we have a button. If the user is a customer already, we want the button to say ‘View Download’ and lead them to their account page to download it. If they aren’t a customer, we want this to be a buy button and lead them to the product page instead.

Inside the options for the button element, both the text and the URL have dynamic data options. We can select Function value return, add our custom function name and provide three arguments. The ID of the download, what to return if the user is a customer, and what to return if they aren’t.

In the above example, for the three arguments..

  1. The ID of the download, let’s say is 1.
  2. The button text to show if the user HAS purchased. ‘View download’
  3. The button text to show if the user HASN’T purchased ‘Buy Now’

We can then do the same for the button URL, using the same function..

This time the three arguments..

  1. The ID of the download, again 1.
  2. The button URL if the user HAS purchased. ‘/account/’
  3. The button URL if the user HASN’T purchased. ‘/product-page/’

Example 2: Is the current page X?

Obviously, the power of this depends on how comfortable you are with writing functions. But there are lots of simple functions already included in WP for the most common things.

For eg is_page(), is probably one of the most commonly used.

The use case; if we have a template that is applied to all pages, for example, it could be a header that is on all pages but some content needs to change only if the page is ‘about’ page. This is where we can use the dynamic data to add our condition.

Here’s the custom function that would work out if we’re on a specific page..

function is_specific_page( $page, $is_page, $isnt_page ) {
	return is_page($page) ? $is_page : $isnt_page;
}

Similar to the last example, if the is_page returns true then our second argument will be returned, if it’s false then the third will be returned.

How to use this in Zion Builder

We’d use it in Zion Builder like this;

If the current page is the page with the slug ‘about’ then the text will say ‘this is about me’. If it isn’t it will say ‘this isn’t about me’

That’s it

Hopefully, this gives you some ideas and a better understanding of how the function return value option can be useful for conditional content.