Custom dynamic tag {current_term_meta:termnameslug:metakey}
for Bricks Builder. Retrieves term meta values by term name and meta key on single posts (using attached terms) and term archives (using queried term). Automatically replaces the tag in dynamic content
<?php /** * ---------------------------------------- * Current Term Meta (Hybrid) Tag * ---------------------------------------- * Usage: {current_term_meta:termname:metakey} * Description: * - On a single post: finds attached term by name and fetches the meta value. * - On a term archive: uses the current queried term and fetches the meta value. * ---------------------------------------- */ // 1. Register tag for Bricks add_filter('bricks/dynamic_tags_list', function($tags) { $tags[] = [ 'name' => '{current_term_meta:termname:metakey}', 'label' => 'Current Term Meta (by Name + Meta Key)', 'group' => 'SNN', ]; return $tags; }); // 2. Logic: get term meta value by context function get_current_term_meta_value($termname, $metakey, $post) { // If on a term archive: use queried object if (is_category() || is_tag() || is_tax()) { $term = get_queried_object(); if ($term && isset($term->term_id) && ($term->name === $termname || $term->slug === $termname)) { return get_term_meta($term->term_id, $metakey, true); } // If $termname is empty, just return meta for current term if ($term && isset($term->term_id) && empty($termname)) { return get_term_meta($term->term_id, $metakey, true); } } // Otherwise, get all taxonomies for the post and find term by name if ($post && isset($post->ID)) { $taxonomies = get_object_taxonomies($post->post_type); foreach ($taxonomies as $taxonomy) { $terms = get_the_terms($post->ID, $taxonomy); if (empty($terms) || is_wp_error($terms)) { continue; } foreach ($terms as $term) { if ($term->name === $termname || $term->slug === $termname) { return get_term_meta($term->term_id, $metakey, true); } } } } return ''; } // 3. Render the tag add_filter('bricks/dynamic_data/render_tag', function($tag, $post, $context = 'text') { // Pattern: {current_term_meta:termname:metakey} if (is_string($tag) && preg_match('/^{current_term_meta:([^:}]+):([^}]+)}/', $tag, $m)) { $termname = sanitize_text_field($m[1]); $metakey = sanitize_key($m[2]); return get_current_term_meta_value($termname, $metakey, $post); } return $tag; }, 20, 3); // 4. Replace in dynamic content add_filter('bricks/dynamic_data/render_content', function($content, $post, $context = 'text') { if (!is_string($content) || strpos($content, '{current_term_meta:') === false) { return $content; } preg_match_all('/{current_term_meta:([^:}]+):([^}]+)}/', $content, $matches, PREG_SET_ORDER); if (!empty($matches)) { foreach ($matches as $m) { $full_tag = $m[0]; $termname = sanitize_text_field($m[1]); $metakey = sanitize_key($m[2]); $value = get_current_term_meta_value($termname, $metakey, $post); $content = str_replace($full_tag, $value, $content); } } return $content; }, 20, 3); add_filter('bricks/frontend/render_data', function($content, $post, $context = 'text') { // Reuse the above logic return apply_filters('bricks/dynamic_data/render_content', $content, $post, $context); }, 20, 2);