Register custom Gutenberg blocks that render server-side using PHP templates. Compatible with acf_register_block_type().
fieldforge_register_block_type( $config )
Both fieldforge_register_block_type() and acf_register_block_type() are supported.
| Parameter | Type | Default | Description | |
|---|---|---|---|---|
name | string | required | Block name (alphanumeric + dashes) | |
title | string | '' | Display title in block inserter | |
description | string | '' | Description in block inspector | |
category | string | 'fieldforge' | Block category slug | |
icon | string | 'admin-generic' | Dashicon name | |
keywords | array | [] | Search keywords (max 3) | |
post_types | array | [] | Restrict to specific post types | |
mode | string | 'preview' | 'preview', 'edit', 'auto' | |
align | string | '' | Default alignment | |
render_template | string | '' | Absolute path to PHP template | |
render_callback | callable | null | null | Callback function |
enqueue_style | string | '' | CSS file URL | |
enqueue_script | string | '' | JS file URL | |
enqueue_assets | callable | null | null | Function for enqueuing assets |
supports | array | [] | WordPress block supports | |
example | array | [] | Preview data for inserter |
render_template Example
php
add_action( 'init', function() {
if ( ! function_exists( 'fieldforge_register_block_type' ) ) {
return;
}
fieldforge_register_block_type( [
'name' => 'hero',
'title' => 'Hero Section',
'icon' => 'cover-image',
'keywords' => [ 'hero', 'banner', 'header' ],
'render_template' => get_template_directory() . '/blocks/hero.php',
'enqueue_style' => get_template_directory_uri() . '/blocks/hero.css',
'supports' => [ 'align' => [ 'full', 'wide' ] ],
] );
} );php
<?php
// blocks/hero.php
// Available variables: $block, $content, $is_preview, $post_id
$heading = get_field( 'heading' );
$bg = get_field( 'background_image' );
$classes = 'hero-block';
if ( ! empty( $block['className'] ) ) {
$classes .= ' ' . $block['className'];
}
?>
<div class="<?php echo esc_attr( $classes ); ?>"
<?php if ( $bg ) : ?>style="background-image: url(<?php echo esc_url( $bg['url'] ); ?>);"<?php endif; ?>>
<?php if ( $is_preview && ! $heading ) : ?>
<p class="placeholder">Add a heading to configure this hero block.</p>
<?php else : ?>
<h1><?php echo esc_html( $heading ); ?></h1>
<p><?php the_field( 'subheading' ); ?></p>
<?php endif; ?>
</div>render_callback Example
php
fieldforge_register_block_type( [
'name' => 'cta-banner',
'title' => 'CTA Banner',
'icon' => 'megaphone',
'render_callback' => 'render_cta_banner_block',
] );
function render_cta_banner_block( $block, $content, $is_preview, $post_id ) {
$heading = get_field( 'heading' ) ?: 'Get Started Today';
$color = get_field( 'background_color' ) ?: '#1a1a2e';
?>
<div class="cta-banner" style="background-color: <?php echo esc_attr( $color ); ?>;">
<h2><?php echo esc_html( $heading ); ?></h2>
<a href="<?php the_field( 'button_link' ); ?>" class="cta-btn"><?php the_field( 'button_text' ); ?></a>
</div>
<?php
}Multiple Blocks Registration
php
add_action( 'init', function() {
if ( ! function_exists( 'fieldforge_register_block_type' ) ) {
return;
}
$blocks = [
[ 'name' => 'testimonial', 'title' => 'Testimonial', 'icon' => 'format-quote' ],
[ 'name' => 'pricing-table', 'title' => 'Pricing Table', 'icon' => 'money-alt' ],
[ 'name' => 'team-member', 'title' => 'Team Member', 'icon' => 'admin-users' ],
];
foreach ( $blocks as $b ) {
$b['render_template'] = get_template_directory() . '/blocks/' . $b['name'] . '.php';
$b['enqueue_style'] = get_template_directory_uri() . '/blocks/' . $b['name'] . '.css';
fieldforge_register_block_type( $b );
}
} );—