Blocks Code
PHP Callback
PHP callbacks let you handle block rendering with custom PHP functions instead of using the block builder's code editor. This approach is perfect for placing block logic in your theme or plugin files.
Basic Usage
Replace my-block
with your block slug and render_my_block
with your unique function name:
functions.php
// Frontend output callback
add_filter('lazyblock/my-block/frontend_callback', 'render_my_block', 10, 2);
// Editor preview callback
add_filter('lazyblock/my-block/editor_callback', 'render_my_block', 10, 2);
if (!function_exists('render_my_block')) :
/**
* Block render callback
*
* @param string $output Default block output
* @param array $attributes Block attributes
*/
function render_my_block($output, $attributes) {
ob_start();
?>
<div class="my-block">
<?php if (isset($attributes['title'])) : ?>
<h2><?php echo esc_html($attributes['title']); ?></h2>
<?php endif; ?>
<?php if (isset($attributes['content'])) : ?>
<div class="content">
<?php echo wp_kses_post($attributes['content']); ?>
</div>
<?php endif; ?>
</div>
<?php
return ob_get_clean();
}
endif;
Custom Wrappers
Block Wrapper
By default, Lazy Blocks adds a wrapper to your block output. Disable it with:
add_filter('lazyblock/my-block/frontend_allow_wrapper', '__return_false');
Inner Blocks Wrapper
For blocks using Inner Blocks, disable the default wrapper:
add_filter('lazyblock/my-block/allow_inner_blocks_wrapper', '__return_false');
Advanced Example
add_filter('lazyblock/team-member/frontend_callback', 'render_team_member', 10, 2);
function render_team_member($output, $attributes) {
// Ensure required data exists
if (!isset($attributes['name']) || !isset($attributes['role'])) {
return '';
}
// Prepare data
$name = esc_html($attributes['name']);
$role = esc_html($attributes['role']);
$image = isset($attributes['image']['url']) ? $attributes['image']['url'] : '';
$bio = isset($attributes['bio']) ? wp_kses_post($attributes['bio']) : '';
ob_start();
?>
<div class="team-member">
<?php if ($image) : ?>
<div class="photo">
<img src="<?php echo esc_url($image); ?>"
alt="<?php echo esc_attr($name); ?>">
</div>
<?php endif; ?>
<div class="info">
<h3 class="name"><?php echo $name; ?></h3>
<div class="role"><?php echo $role; ?></div>
<?php if ($bio) : ?>
<div class="bio"><?php echo $bio; ?></div>
<?php endif; ?>
</div>
</div>
<?php
return ob_get_clean();
}
Use PHP callbacks when you need:
- Complex dynamic content
- Integration with other WordPress functions
- Custom database queries
- Advanced data processing