Custom Render PHP
Custom Render PHP allows complete control over block output. Use this when you need custom PHP logic that Twig templates can’t handle.
When to Use Custom Render PHP
Section titled “When to Use Custom Render PHP”Use custom render.php when you need:
- Complex PHP logic
- WordPress function integration
- Custom database queries
- External API calls
- Performance-critical rendering
Enabling Custom Render Mode
Section titled “Enabling Custom Render Mode”- In block editor, go to Settings (wbs section)
- Enable Custom Render Mode
render.php is only used when Custom Render Mode is enabled. Otherwise, blocks use Twig templates.
Creating render.php
Section titled “Creating render.php”<?php/** * Custom render template for testimonial block * $attributes contains all block attribute values */
$quote = isset($attributes['quote']) ? $attributes['quote'] : '';$author = isset($attributes['author']) ? $attributes['author'] : '';$style = isset($attributes['textColor']) ? 'color: ' . esc_attr($attributes['textColor']) : '';?>
<div class="testimonial-block" style="<?php echo esc_attr($style); ?>"> <?php if ($quote) : ?> <blockquote><?php echo wp_kses_post($quote); ?></blockquote> <?php endif; ?>
<?php if ($author) : ?> <cite><?php echo esc_html($author); ?></cite> <?php endif; ?></div>Available Variables
Section titled “Available Variables”| Variable | Description |
|---|---|
$attributes | All block attribute values |
$context | Additional context data |
$block | Block data array |
Security Considerations
Section titled “Security Considerations”- Always escape output with
esc_html(),esc_attr(),wp_kses_post() - Sanitize inputs before using in queries
- Use WordPress functions for data access
Example: Dynamic Content
Section titled “Example: Dynamic Content”<?php/** * Block with dynamic post content */
$post_id = isset($attributes['postId']) ? intval($attributes['postId']) : 0;$post = get_post($post_id);
if ($post) : setup_postdata($post); ?> <article class="featured-post"> <h2><?php the_title(); ?></h2> <div class="content"><?php the_content(); ?></div> </article> <?php wp_reset_postdata();endif;Pro Feature
Section titled “Pro Feature”The Custom Render PHP is only available in WPBits Block Studio Pro. See Pro Features for more information.
Next Steps
Section titled “Next Steps”- Twig Templates — Default template system
- Export Templates — Deploy templates with your blocks