PHP code example of juvo / bricks-dynamic-data-tags

1. Go to this page and download the library: Download juvo/bricks-dynamic-data-tags library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

juvo / bricks-dynamic-data-tags example snippets


add_action('init', function() {
    juvo\Bricks_Dynamic_Data_Tags\DDT_Registry::getInstance();
});

DDT_Registry::getInstance()
    ->set('my_tag', 'My Tag', 'My Tag Group', function($post, $context, array $filters = []) {
        return "Hello World";
    });

DDT_Registry::getInstance()
    ->set('my_tag2', 'My Tag 2', 'My Tag Group', function($post, $context, array $filters = []) {
        return "Hello World 2";
    });

apply_filters('juvo/dynamic_tags/register', $tags);

// Adds "modifier" as a named capturing group to the tag pattern
add_filter("juvo/dynamic_data_tag/parse_tag", function($pattern, $tag) {
    $pattern = str_replace("}/", "", $pattern);
    return $pattern . "(\~(?<modifier>[0-9a-zA-Z_-]+(\~[0-9a-zA-Z_-]+)*))?}/";
}, 10, 2);

apply_filters("juvo/dynamic_data_tag/parse_tag/$tag", $pattern, $this->tag);

add_filter("juvo/dynamic_data_tag/parse_tag/pattern_modifier", function($value) {
    return explode("~", $value);
});

DDT_Registry::getInstance()->set( 'single_tag', 'Single Tag', 'Custom Tags', function( $post, $context, array $filters = [] ) {
    // Parse filters to be key-value pairs
    $parsed_filters = [];
    foreach ( $filters as &$item ) {
        list( $key, $value ) = explode( '=', $item );
        $parsed_filters[ $key ] = $value;
    }
    $filters = $parsed_filters;
    
    $tags = get_the_terms( get_the_ID(), 'post_tag' );
    if ( empty( $tags ) || is_wp_error( $tags ) ) {
        return '';
    }
    
    // Filter to select a specific tag
    $selected_tag = $filters['tag'] ?? '';
    $output       = [];
    
    foreach ( $tags as $tag ) {
        if ((!empty($selected_tag) && $tag->slug === $selected_tag) || empty($selected_tag)) {
    
            // Check if we need links or not
            if ( isset( $filters['link'] ) && $filters['link'] === 'true' ) {
                $output[] = '<a href="' . esc_url( get_term_link( $tag ) ) . '">' . esc_html( $tag->name ) . '</a>';
            } else {
                $output[] = esc_html( $tag->name );
            }
        }
    }
    
    return implode( ', ', $output );
    } );

add_filter("wp_kses_allowed_html", function($allowedtags, $context) {
    if ($context !== "juvo/dynamic_data_tag") {
        return $allowedtags;
    }
    $allowedtags['iframe'] = [
        'src' => true,
        'width' => true,
        'height' => true,
        'frameborder' => true,
        'allow' => true,
        'allowfullscreen' => true,
        'title' => true,
    ];
    return $allowedtags;
        }, 10, 2);

$allowed_tags = wp_kses_allowed_html("juvo/dynamic_data_tag");
$allowed_tags = apply_filters("juvo/dynamic_data_tag/allowed_html_tags/$tag", $allowed_tags);

// Register '{collection}' tag
DDT_Registry::getInstance()
    ->set('collection', 'Collection', 'Collections', function($post, $context, array $filters = []) {
        return "<iframe src='" . get_permalink($post) . "'></iframe>";
    });

// Add custom allowed html tags for the collection tag. In this case we allow iframes.
add_filter("juvo/dynamic_data_tag/allowed_html_tags/collection", function($allowedtags) {
    $allowedtags['iframe'] = [
        'src' => true,
        'width' => true,
        'height' => true,
        'frameborder' => true,
        'allow' => true,
        'allowfullscreen' => true,
        'title' => true,
    ];
    return $allowedtags;
});

DDT_Registry::getInstance()->set(
    'post_data',
    'Post Data',
    'Posts',
    function( \WP_Post $post, string $context, array $filters= [], array $bold = [] ) {

        $output = [];
        foreach ( $filters as $filter ) {
            switch ( $filter ) {
                case 'title':
                    $output['title'] = get_the_title();
                    break;
                case 'excerpt':
                    $output['excerpt'] = get_the_excerpt();
                    break;
                case 'content':
                    $output['content'] = get_the_content();
                    break;
                case 'post_type':
                    $output['post_type'] = get_post_type();
                    break;
            }
        }

        // Add some of the filtered values tobe bold
        foreach($bold as $key) {
            if (in_array($key, $filters)) {
                $output[$key] = "<b>".$output[$key]."</b>";
            }
        }

        return implode( ', ', $output );
    }
);

// Add a custom pattern to introduce a "bold" capture group.
add_filter("juvo/dynamic_data_tag/parse_tag/post_data", function($pattern, $tag) {
    $pattern = str_replace("}/", "", $pattern);
    return $pattern . "(?:~bold=(?<bold>[0-9a-zA-Z_-]+))*}/"; // use ~bold= as separator
}, 10, 2);

// To allow the freshly added "bold" capture group to have multiple values we need to split the values by the separator
add_filter("juvo/dynamic_data_tag/parse_tag/pattern_bold", function($value) {
    return explode("~bold=", $value);
});

DDT_Registry::getInstance()->set( 'single_tag', 'Single Tag', 'Custom Tags', function( $post, $context, array $filters = [], array $modifier = [] )