Let’s explore how GenerateBlocks implements dynamic tags and how we can create our own following similar patterns. We’ll break down the official GenerateBlocks examples and create our own custom implementations.
Note: I should mention that since there's currently no official documentation for GenerateBlocks dynamic tags, I've had to reverse engineer the code to understand how it works. While I'll share my understanding of the parameters and capabilities, there may be features I haven't fully discovered yet. I'll update this guide as I learn more.
Understanding the Core Parameters
Before diving into specific examples, let’s understand the common parameters used in all dynamic tags:
title:
The name that appears in the dynamic tag selector in the block editortag:
A unique identifier for your dynamic tagtype:
Groups tags in the interface (e.g., ‘post’, ‘author’, ‘elements’)supports:
Additional features the tag supports (e.g., ‘source’, ‘image-size’)options:
Configuration options available for the tagreturn:
Callback function that generates the output
GenerateBlocks Built-in Examples
Post Permalink Tag
new GenerateBlocks_Register_Dynamic_Tag( [ 'title' => __('Post Permalink', 'generateblocks'), 'tag' => 'post_permalink', 'type' => 'post', 'supports' => ['source'], 'return' => ['GenerateBlocks_Dynamic_Tag_Callbacks', 'get_the_permalink'], ] );
Copy
Key features:
- Simple implementation with no options
- ‘source’ support enables basic functionality
- Returns the post’s permanent URL
- No additional configuration needed
Featured Image Tag
new GenerateBlocks_Register_Dynamic_Tag( [ 'title' => __('Featured Image', 'generateblocks'), 'tag' => 'featured_image', 'type' => 'post', 'supports' => ['source', 'image-size'], 'options' => [ 'key' => [ 'type' => 'select', 'label' => __('Image Key', 'generateblocks'), 'default' => 'url', 'options' => [ 'url', 'id', 'caption', 'description', 'alt', ], ], ], 'return' => ['GenerateBlocks_Dynamic_Tag_Callbacks', 'get_featured_image'], ] );
Copy
Key features:
- Supports ‘image-size’ for different thumbnail sizes
- Options are presented as a dropdown menu
- Returns different image attributes based on selection
- Uses GenerateBlocks’ built-in callback method
Custom Implementations
Here are our custom implementations following similar patterns:
Post Content Tag
new GenerateBlocks_Register_Dynamic_Tag( [ 'title' => __('Post Content', 'generateblocks'), 'tag' => 'post_content', 'type' => 'post', 'supports' => ['source'], 'options' => [ 'strip_shortcodes' => [ 'type' => 'checkbox', 'label' => __('Strip Shortcodes', 'your-textdomain'), 'default' => false, ], 'apply_filters' => [ 'type' => 'checkbox', 'label' => __('Apply Content Filters', 'your-textdomain'), 'default' => true, ], ], 'return' => 'custom_post_content_callback', ] ); function custom_post_content_callback($options, $block, $instance) { $content = get_the_content(); if (!empty($options['strip_shortcodes'])) { $content = strip_shortcodes($content); } if (!empty($options['apply_filters'])) { $content = apply_filters('the_content', $content); } return GenerateBlocks_Dynamic_Tag_Callbacks::output($content, $options, $instance); }
Copy
Key features:
- Two checkbox options for content processing
- Help text explains each option
- Processes content based on selected options
- Uses WordPress core functions
Custom Hook Tag
new GenerateBlocks_Register_Dynamic_Tag( [ 'title' => __('Custom Hook', 'your-textdomain'), 'tag' => 'custom_hook', 'type' => 'elements', 'supports' => ['source'], 'options' => [ 'hook_name' => [ 'type' => 'text', 'label' => __('Hook Name', 'your-textdomain'), 'help' => __('Enter the WordPress action hook name', 'your-textdomain'), 'placeholder' => 'your_custom_hook', ], 'priority' => [ 'type' => 'number', 'label' => __('Priority', 'your-textdomain'), 'default' => 10, ], ], 'return' => 'custom_hook_callback', ] ); function custom_hook_callback($options, $block, $instance) { if (empty($options['hook_name'])) { return ''; } ob_start(); do_action( $options['hook_name'], $options['priority'] ?? 10 ); $output = ob_get_clean(); return GenerateBlocks_Dynamic_Tag_Callbacks::output($output, $options, $instance); }
Copy
Key features:
- Text input for hook name
- Number input for priority
- Checks for empty hook name
- Uses output buffering to capture hook content
GenerateBlocks Output Callback Helper Function
public static function output($output, $options, $instance = null) { $raw_output = $output; $output = self::with_trunc($output, $options); // Truncates content $output = self::with_replace($output, $options); // String replacements $output = self::with_trim($output, $options); // Trims whitespace $output = self::with_case($output, $options); // Changes text case // Formatting filters $output = self::with_wpautop($output, $options); // Adds paragraphs $output = self::with_link($output, $options, $instance); // Wraps in link return apply_filters('generateblocks_dynamic_tag_output', $output, $options, $raw_output); }
Copy
When we use GenerateBlocks_Dynamic_Tag_Callbacks::output
, our content gets these automatic processing features:
with_trunc
: Truncates content to specified lengthwith_replace
: Performs string replacementswith_trim
: Removes extra whitespacewith_case
: Modifies text case (upper/lower/title)with_wpautop
: Adds paragraph tagswith_link
: Wraps content in links if specified
These processes run in order, ensuring consistent output across all dynamic tags.