Enregistrez des blocs Gutenberg personnalisés qui se rendent côté serveur en utilisant des modèles PHP. Compatible avec acf_register_block_type().
fieldforge_register_block_type( $config )
Les fieldforge_register_block_type() et acf_register_block_type() sont tous deux pris en charge.
| Paramètre | Type | Par défaut | Description | |
|---|---|---|---|---|
name | string | requis | Nom du bloc (alphanumérique + tirets) | |
title | string | '' | Titre d’affichage dans l’inserteur de blocs | |
description | string | '' | Description dans l’inspecteur de blocs | |
category | string | 'fieldforge' | Slug de catégorie de bloc | |
icon | string | 'admin-generic' | Nom de Dashicon | |
keywords | array | [] | Mots-clés de recherche (max 3) | |
post_types | array | [] | Restreindre à des types de publication spécifiques | |
mode | string | 'preview' | 'preview', 'edit', 'auto' | |
align | string | '' | Alignement par défaut | |
render_template | string | '' | Chemin absolu vers le modèle PHP | |
render_callback | callable\ | null | null | Fonction de rappel |
enqueue_style | string | '' | URL du fichier CSS | |
enqueue_script | string | '' | URL du fichier JS | |
enqueue_assets | callable\ | null | null | Fonction pour l’enregistrement des ressources |
supports | array | [] | Prise en charge des blocs WordPress | |
example | array | [] | Données d’aperçu pour l’inserteur |
exemple de 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>exemple de 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
}Enregistrement de plusieurs blocs
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 );
}
} );—