How to create numerous dynamic data tags?

Hi! Novice developer here.

I am working on integrating my simple plugin with Bricks.

I’m following the Create Your Own Dynamic Data Tag tutorial , and after trial and error managed to get this to work! Yay.

However to my eyes it seems like a lot of code just to create one tag (I will need to make at least 12).

Do I need to duplicate all these code blocks for as many tags as I want to include from my plugin? Or is there a way to include multiple tag names more efficiently?

Please let us know best practices (an example please) for creating numerous dynamic data tags.

Thank you so much! :hugs:

1 Like

here example

paste this to AI and ask if you need similar dynamic tag

this example is for getting category slug. I use this on attributes time to time

2 Likes

I think I figured it out.
It’s not 100% Clear to be honest, but after some fiddling, this worked for me: I now have 2 dynamic tags in the group:

Screenshot 2024-04-15 at 21.08.25

<?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'  => '{tsNumber}',
        'label' => 'tsNumber',
        'group' => 'Reno Dynamic data',
    ];
    $tags[] = [
        'name'  => '{tsHasSite}',
        'label' => 'tsHasSite',
        'group' => 'Reno Dynamic data',
    ];

    return $tags;
}

add_filter( 'bricks/dynamic_data/render_tag', 'get_my_tag_value', 10, 3 );
function get_my_tag_value( $tag, $post, $context = 'text' ) {
    // $tag is the tag name without the curly braces
    $value = '';
    // Do your custom logic here
    if ($tag === 'tsNumber') {
        $value = runTsNumLogic();
    } else if ($tag === 'tsHasSite') {
        $value = runCatLogic();
    }
    return $value;
}

function runTsNumLogic() {
    // Do your custom logic here
    $post_id = get_the_ID();
    $tsStr = get_field( "partner_ts", $post_id);
    $tsNum = floatval($tsStr);

    return $tsNum;
}

function runCatLogic() {
    // Do your custom logic here
    $hasSite = 'true';
    $categories = get_the_category();
    if ($categories) {
        foreach ($categories as $category) {
            if ($category->slug === 'no-site') {
                $hasSite = 'false';
            }
        }
    }

    return $hasSite;
}

add_filter( 'bricks/dynamic_data/render_content', 'render_my_tag', 10, 3 );
add_filter( 'bricks/frontend/render_data', 'render_my_tag', 10, 2 );
function render_my_tag( $content, $post, $context = 'text' ) {
    // $content might consist of HTML and other dynamic tags
    // Only look for dynamic tags {tsNumber} and {tsHasSite}
    if ( strpos( $content, '{tsNumber}' ) !== false ) {
        // Do your custom logic here for tsNumber tag
        $my_value = runTsNumLogic();
        // Replace the tag with the value you want to display
        $content = str_replace( '{tsNumber}', $my_value, $content );
    }
    if ( strpos( $content, '{tsHasSite}' ) !== false ) {
        // Do your custom logic here for tsHasSite tag
        $my_value = runCatLogic();
        // Replace the tag with the value you want to display
        $content = str_replace( '{tsHasSite}', $my_value, $content );
    }
    return $content;
}
2 Likes

Thanks both!! I will review this.

Update: I heard back from our friendly neighbourhood Bricks team with a general recommendation; it looks somewhat similar to what you are each (no pun intended on the foreach, hehe) doing:

You are right, you shouldn’t copy and paste the code 12 times for those 12 fields.

You should use some PHP logic like foreach() to loop through an array with all 12 keys to register the dynamic tags.

Same when rendering, you can use the PHP switch() function to conditionally parse the dynamic tags, etc.

Also, they might be updating their documentation eventually to account for this (IMO more common) use case of needing to create numerous tags efficiently.

Again, my use case is to share data from my plugin, which allows the user to add global information relevant to the website (name, address [street, city, state, zip], phone, etc.) that I would like to use with Bricks.

Hi

this is how I did it:

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',
  ];
   $tags[] = [
     'name'  => '{chochete_url}',
     'label' => 'Chochete URL',
     '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}' && $tag !== '{chochete_url}') {
    return $tag;
  }
  // Do your custom logic here, you should define run_my_dd_tag_logic() function
  switch ($tag) {
      case '{kk_star-rating}': $value = run_rating_dd_tag_logic();
      break;
      case '{chochete_url}': $value = run_chocheteurl_dd_tag_logic();
      break;
  }
  return $value;
}

function run_rating_dd_tag_logic() {
  $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;
  return $avg;
}
function run_chocheteurl_dd_tag_logic() {
  $chochete_relation = get_field('die_chochete_im_kalender_relation');
      if (!empty($chochete_relation)) {
         foreach ($chochete_relation as $chochete) {
          return get_permalink($chochete->ID);
         }
      }  else {
          $kalenderjahr = substr(get_the_title(),0,4); 
          $chochete_url = 'https://www.test.ch/kalender/#cal'.$kalenderjahr;
          return $chochete_url;
      }
}
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 && strpos( $content, '{chochete_url}' ) === false   ) {
    return $content;
  }

    switch (true) {
        case strpos( $content, '{kk_star-rating}' ): 
        $my_value = run_rating_dd_tag_logic();
        $content = str_replace( '{kk_star-rating}', $my_value, $content );
        break;
        case strpos( $content, '{chochete_url}' ): 
         $my_value = run_chocheteurl_dd_tag_logic();
        $content = str_replace( '{chochete_url}', $my_value, $content );
        break;
    }
  return $content;
}

Cheers

Patric