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.
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:
<?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;
}
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.