NO BUG: Filtering Strips Dynamic Tags from Class List

Browser: Chrome 110
OS: macOS
Video: Jam

Hi,

I have created a custom dynamic data tag which adds the custom taxonomy assigned to a post as a string that I add as a class to a loop item. I then use that class to style each individual post.

I have an archive page for a custom post type call ‘Work’ that displays a grid of query loop items. There is also a Bricks filter which allows users to filter by my custom taxonomies. The problem I have is that when I apply any filter, the classes applied using my dynamic tag are stripped out. Any tags that are manually added in are still shown.

Here is the code I am using to create the dynamic tag (which works great on first load) which follows the Bricks documentation guidance:

// Custom Bricks tag to add assigned Theme terms as a class
add_filter( 'bricks/dynamic_tags_list', 'add_my_tag_to_builder' );
function add_my_tag_to_builder( $tags ) {
  $tags[] = [
    'name'  => '{my_term_slug_tag}',
    'label' => 'Work Theme Slug',
    'group' => 'Custom data Group',
  ];
  return $tags;
}

// Define how the custom tag gets its value
add_filter( 'bricks/dynamic_data/render_tag', 'get_my_tag_value', 20, 3 );
function get_my_tag_value( $tag, $post, $context = 'text' ) {
  if ( $tag !== '{my_term_slug_tag}' ) {
    return $tag;
  }
  return run_my_term_slug_tag_logic();
}

// Function to fetch the term slugs as classes
function run_my_term_slug_tag_logic() {
  if ( is_archive( 'work' ) || is_page( '2230' ) ) {
    $terms = get_the_terms( get_the_ID(), 'work_theme' );
    $classes = [];
    if ( $terms && ! is_wp_error( $terms ) ) {
      foreach ( $terms as $term ) {
        $classes[] = 'sf-' . sanitize_html_class( $term->slug );
      }
    }
    return implode( ' ', $classes );
  }
  return ''; // Default return value if no terms found
}

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' ) {
  if ( strpos( $content, '{my_term_slug_tag}' ) === false ) {
    return $content;
  }
  $my_value = run_my_term_slug_tag_logic();
  return str_replace( '{my_term_slug_tag}', $my_value, $content );
}

The video shows the classes on load (you can see the coloured border at the top of each box is reset to a uniform colour when any filter is applied and the class removed in the isnpector).

Can anyone suggest a solution?

Hello,

I am not sure if I would classify this as a bug - if you just return one simple string, it should work, right? I believe the issue is in your “if” condition. Because the filters will load the content via Ajax, not everything is available.

You do have the access to the $post (if you pass it from get_my_tag_value and render_my_tag), so that may help you get what you need.

Matej

1 Like

Perfect! @Matej – the problem was indeed the IF statement. I’ve adjusted that function and it’s all working perfectly. Thanks so much! :raised_hands:

Here’s the revised code in case it comes in useful for anybody else:

// Function to fetch the term slugs as classes
function run_my_term_slug_tag_logic( $post = null ) {
if ( ! $post ) {
$post = get_post();
}

$terms = get_the_terms( $post->ID, 'work_theme' );
$classes = [];

if ( $terms && ! is_wp_error( $terms ) ) {
    foreach ( $terms as $term ) {
        $classes[] = 'sf-' . sanitize_html_class( $term->slug );
    }
}

return implode( ' ', $classes );

}

1 Like

Perfect, I’m glad that you got it working. I’ll mark it as no bug then :slight_smile:

Matej

1 Like