ARTICLES

Retrieve Dynamic Data Like a Pro: Using bricks_render_dynamic_data in Your Custom PHP Functions

The {echo:} dynamic tag is a powerful feature in Bricks. With it, you can easily execute your desired functions or transform data on the frontend.

However, issues may arise when using {echo:} with dynamic parameters.

Instead of passing parameters to your custom PHP function, I suggest using the bricks_render_dynamic_data function (@since 1.5.5) within your PHP function.

Here’s an example:

Let’s say we have a custom function that says “Hi” if field A is ‘yes’, otherwise it says “Hello” to field B:

// Say Hi if field A is 'yes', otherwise say Hello to field B
function conditionally_say_hi( $field_a, $field_b ) {
	if ( $field_a === 'yes' ) {
		return 'Hi';
	} else {
		return 'Hello ' . $field_b;
	}
}

Inside the builder, we would use {echo:conditionally_say_hi( {acf_field_a}, {acf_field_b} )} .

However, sometimes you may get unexpected results, such as incorrect values or parameters.

To solve this, you can easily modify your function like this and use {echo:conditionally_say_hi} directly inside the builder:

// Say Hi if field A is 'yes', otherwise say Hello to field B
function conditionally_say_hi() {
	$field_a = bricks_render_dynamic_data( '{acf_field_a}' );
	$field_b = bricks_render_dynamic_data( '{acf_field_b}' );

	// You can get more parameters as you like

	if ( $field_a === 'yes' ) {
		return 'Hi';
	} else {
		return 'Hello ' . $field_b;
	}
}

By doing this, you won’t need to pass too many parameters from the builder and clutter your fields. Additionally, you’ll only need to change the conditionally_say_hi function in your script, which can include adding or changing parameters, without the need to change every field inside the builder.

You might like these 2 articles as well:

  • About Author

    Jenn@Itchycode
    I like to solve problems by coding. I like websites, web applications and everything viewable from browser. Knowledge sharing can grows my confidence. A simple "thank you" will make my day :)
    More about me

Subscribe For Notification

Get notification whenever new article published.
Subscription Form

Discussion

COMMENT
Be the first to drop a comment.
New comment
Comment Form