Регистрируйте пользовательские блоки Gutenberg, которые отображаются на стороне сервера с использованием PHP-шаблонов. Совместимо с acf_register_block_type().
fieldforge_register_block_type( $config )
Поддерживаются как fieldforge_register_block_type(), так и acf_register_block_type().
| Параметр | Тип | По умолчанию | Описание | |
|---|---|---|---|---|
name | string | обязательный | Имя блока (буквы + цифры + дефисы) | |
title | string | '' | Отображаемое название в вставщике блоков | |
description | string | '' | Описание в инспекторе блоков | |
category | string | 'fieldforge' | Слаг категории блока | |
icon | string | 'admin-generic' | Имя Dashicon | |
keywords | array | [] | Ключевые слова для поиска (макс 3) | |
post_types | array | [] | Ограничить для конкретных типов записей | |
mode | string | 'preview' | 'preview', 'edit', 'auto' | |
align | string | '' | Выравнивание по умолчанию | |
render_template | string | '' | Абсолютный путь к PHP-шаблону | |
render_callback | callable\ | null | null | Функция обратного вызова |
enqueue_style | string | '' | URL файла CSS | |
enqueue_script | string | '' | URL файла JS | |
enqueue_assets | callable\ | null | null | Функция для подключения ресурсов |
supports | array | [] | Поддержка блоков WordPress | |
example | array | [] | Данные предварительного просмотра для вставщика |
render_template Пример
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 Пример
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
}Регистрация нескольких блоков
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 );
}
} );—