Dynamic Data Integration
warning
Dynamic output can introduce security risks if you render unescaped user content. Treat dynamic data like production code: sanitize inputs and escape outputs.
Dynamic Data Integration Explained
| Source | Examples | Common tools |
|---|---|---|
| Core post fields | title, excerpt, author, date | WordPress template tags |
| Custom fields | ACF/meta values | get_post_meta() / field plugins |
| Queries | latest posts, related posts | Query blocks / custom loops |
| Shortcodes | plugin-generated output | do_shortcode() (use carefully) |
Why It Matters
- Dynamic content turns static sections into reusable templates.
- You can build archive and listing pages without writing full custom templates.
- Proper scoping prevents "dynamic everywhere" bugs.
How It Works
Dynamic data is resolved at render time (front end) based on the current request context. GenerateBlocks and other block tools may provide a UI to bind fields to block attributes.
flowchart LR
REQ[Request context] --> RESOLVE[Resolve dynamic fields]
RESOLVE --> OUT[Rendered HTML]
Practical Walkthrough
Step 1: Locate Dynamic-Related Code Paths (Source Hints)
locate-dynamic-folders.sh
cd /var/www/html
ls -lah wp-content/plugins/generateblocks/includes/dynamic 2>/dev/null | head -n 60 || true
ls -lah wp-content/plugins/generateblocks/includes/render 2>/dev/null | head -n 60 || true
Step 2: Search for Dynamic Features in the Codebase
search-dynamic-features.sh
cd /var/www/html
grep -R "dynamic\|render\|meta" -n wp-content/plugins/generateblocks/includes 2>/dev/null | head -n 80 || true
Step 3: Identify Custom Fields Used on a Post (Optional)
inspect-post-meta.sh
cd /var/www/html
# Replace 123 with a real post ID
wp post meta list 123 2>/dev/null | head -n 40 || true
Step 4: Confirm the Current Post Types (Context Matters)
list-post-types.sh
cd /var/www/html
wp post-type list --fields=name,label | head -n 40
Practical Examples
Example 1: A Dynamic "Post Meta" Row
Use a small block section to show:
- Publish date
- Reading time (if available)
- Category badges
Keep it consistent across posts.
Example 2: A Query-Driven "Latest Posts" Section
Goal: one reusable section that renders the latest 3 posts.
- Use a query-based block or a block element.
- Keep thumbnails optimized and consistent.
- Avoid deep nesting.
Example 3: Render a Custom Field Safely (Code Pattern)
wp-content/themes/generatepress-child/functions.php
<?php
add_shortcode( 'safe_meta', function( $atts ) {
$atts = shortcode_atts( array( 'key' => '' ), $atts );
$value = get_post_meta( get_the_ID(), $atts['key'], true );
return esc_html( (string) $value );
} );
Then use [safe_meta key="my_field"] in a block if needed.
Example 4: Dynamic Safety Checklist
| Risk | Example | Mitigation |
|---|---|---|
| XSS | unescaped meta output | esc_html() / wp_kses_post() |
| Slow queries | uncached loops | limit results, cache where possible |
| Wrong context | field used on archives | scope element rules to correct templates |
Best Practices
| Practice | Why |
|---|---|
| Keep dynamic sections scoped | Prevents incorrect output on other templates |
| Escape output | Avoid XSS and layout breakage |
| Cache where appropriate | Dynamic queries can be expensive |
| Document dynamic dependencies | Helps debugging (ACF, plugins, shortcodes) |
| Test multiple post types | Context changes per template |
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Field shows empty | Meta key mismatch | List meta keys with wp post meta list |
| Query section slow | Uncached query | Limit results and add caching strategies |
| Output differs by page | Context mismatch | Confirm where the element is displayed |
| Field shows raw shortcode | Shortcodes disabled | Enable shortcodes intentionally and avoid nested shortcodes |
Hands-On
- Pick one post type and list its meta keys.
- Build one dynamic section (post meta or latest posts).
- Verify output on at least 3 different posts.
- Test the same section on an archive page and confirm it does not render incorrectly.
Quick Reference
dynamic-data-cheatsheet.sh
cd /var/www/html
grep -R "dynamic" -n wp-content/plugins/generateblocks/includes 2>/dev/null | head
What's Next
- Next: Global Styles & Presets
- Related: Hook Elements