Skip to main content

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

SourceExamplesCommon tools
Core post fieldstitle, excerpt, author, dateWordPress template tags
Custom fieldsACF/meta valuesget_post_meta() / field plugins
Querieslatest posts, related postsQuery blocks / custom loops
Shortcodesplugin-generated outputdo_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

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

RiskExampleMitigation
XSSunescaped meta outputesc_html() / wp_kses_post()
Slow queriesuncached loopslimit results, cache where possible
Wrong contextfield used on archivesscope element rules to correct templates

Best Practices

PracticeWhy
Keep dynamic sections scopedPrevents incorrect output on other templates
Escape outputAvoid XSS and layout breakage
Cache where appropriateDynamic queries can be expensive
Document dynamic dependenciesHelps debugging (ACF, plugins, shortcodes)
Test multiple post typesContext changes per template

Troubleshooting

SymptomCauseFix
Field shows emptyMeta key mismatchList meta keys with wp post meta list
Query section slowUncached queryLimit results and add caching strategies
Output differs by pageContext mismatchConfirm where the element is displayed
Field shows raw shortcodeShortcodes disabledEnable shortcodes intentionally and avoid nested shortcodes

Hands-On

  1. Pick one post type and list its meta keys.
  2. Build one dynamic section (post meta or latest posts).
  3. Verify output on at least 3 different posts.
  4. 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