27. PHP Blocks for Gutenberg | Field Forge - Custom Fields, Built for Speed
Download Log in

27. PHP Blocks for Gutenberg

Developer Guide

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.

Full Parameter Reference:
ParameterTypeDefaultDescription
namestringrequiredBlock name (alphanumeric + dashes)
titlestring''Display title in block inserter
descriptionstring''Description in block inspector
categorystring'fieldforge'Block category slug
iconstring'admin-generic'Dashicon name
keywordsarray[]Search keywords (max 3)
post_typesarray[]Restrict to specific post types
modestring'preview''preview', 'edit', 'auto'
alignstring''Default alignment
render_templatestring''Absolute path to PHP template
render_callbackcallablenullnullCallback function
enqueue_stylestring''CSS file URL
enqueue_scriptstring''JS file URL
enqueue_assetscallablenullnullFunction for enqueuing assets
supportsarray[]WordPress block supports
examplearray[]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 );
    }
} );

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs