Variable $content is empty inside render_my_tag function in dynamic data

I’m adding bricks builder support to my WordPress plugin. I have followed the documentation however I’m still facing issue.

Inside “bricks-child” theme in the functions.php file I have the following code.

<?php

add_filter( 'bricks/dynamic_tags_list', 'add_my_tag_to_builder' );
function add_my_tag_to_builder( $tags ) {
	// Ensure your tag is unique (best to prefix it)
	$tags[] = [
		'name'  => '{my_dd_tag}',
		'label' => 'My Dynamic Data',
		'group' => 'My Dynamic Data Group',
	];

	return $tags;
}

add_filter( 'bricks/dynamic_data/render_tag', 'get_my_tag_value', 20, 3 );
function get_my_tag_value( $tag, $post, $context = 'text' ) {
	// $tag is the tag name with the curly braces after priority 10
	// Only look for dynamic tag my_dd_tag
	if ( $tag !== '{my_dd_tag}' ) {
		return $tag;
	}

	// Do your custom logic here, you should define run_my_dd_tag_logic() function
	$value = run_my_dd_tag_logic();

	return $value;
}

function run_my_dd_tag_logic() {
	// Do your custom logic here
	$my_value = 'My dynamic data value:';

	return $my_value;
}

add_filter( 'bricks/dynamic_data/render_content', 'render_my_tag', 20, 3 );
add_filter( 'bricks/frontend/render_data', 'render_my_tag', 20, 2 );
function render_my_tag( $content, $post, $context = 'text' ) {
	var_dump($content); //Returns: string(0) ""

	// $content might consists of HTML and other dynamic tags
	// Only look for dynamic tag {my_dd_tag}
	if ( strpos( $content, '{my_dd_tag}' ) === false ) {
		return $content;
	}

	// Do your custom logic here, you should define run_my_dd_tag_logic() function
	$my_value = run_my_dd_tag_logic();

	// Replace the tag with the value you want to display
	$content = str_replace( '{my_dd_tag}', $my_value, $content );

	return $content;
}

Inside the function render_my_tag, this line var_dump($content); returns string(0) ""

What am I doing wrong?

Hi

this is my code for a dynamic data tag {kk_star-rating} and it works. Maybe it helps you.

add_filter( 'bricks/dynamic_tags_list', 'add_starrating_tag_to_builder' );
function add_starrating_tag_to_builder( $tags ) {
  // Ensure your tag is unique (best to prefix it)
  $tags[] = [
    'name'  => '{kk_star-rating}',
    'label' => 'kk Star Rating',
    'group' => 'My Dynamic Data Group',
  ];

  return $tags;
}
add_filter( 'bricks/dynamic_data/render_tag', 'get_starrating_tag_value', 20, 3 );
function get_starrating_tag_value( $tag, $post, $context = 'text' ) {
  // $tag is the tag name with the curly braces after priority 10
  // Only look for dynamic tag my_dd_tag
  if ( $tag !== '{kk_star-rating}' ) {
    return $tag;
  }

  // Do your custom logic here, you should define run_my_dd_tag_logic() function
  $value = run_rating_dd_tag_logic();

  return $value;
}

 function run_rating_dd_tag_logic() {
  // Do your custom logic here
  
  $best  = get_option('kksr_stars');
  $score = get_post_meta(get_the_ID(), '_kksr_ratings', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_ratings', true)) : 0;
  $votes = get_post_meta(get_the_ID(), '_kksr_casts', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_casts', true)) : 0;
  $avg = $score && $votes ? round((float)(($score/$votes)*($best/5)), 1) : 0;
  $my_value = $avg;
  return $my_value;
}

add_filter( 'bricks/dynamic_data/render_content', 'render_kkstar_rating', 20, 3 );
add_filter( 'bricks/frontend/render_data', 'render_kkstar_rating', 20, 2 );
function render_kkstar_rating( $content, $post, $context = 'text' ) {

  // $content might consists of HTML and other dynamic tags
  // Only look for dynamic tag {my_dd_tag}
  if ( strpos( $content, '{kk_star-rating}' ) === false ) {
    return $content;
  }
 
  // Do your custom logic here, you should define run_my_dd_tag_logic() function
  $my_value = run_rating_dd_tag_logic();
 
  // Replace the tag with the value you want to display
  $content = str_replace( '{kk_star-rating}', $my_value, $content );

  return $content;
}
1 Like