27. Blocs PHP pour Gutenberg | Field Forge - Champs personnalisés, conçus pour la vitesse
Télécharger Se connecter

27. Blocs PHP pour Gutenberg

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.

Référence complète des paramètres :
ParamètreTypePar défautDescription
namestringrequisNom du bloc (alphanumérique + tirets)
titlestring''Titre d’affichage dans l’inserteur de blocs
descriptionstring''Description dans l’inspecteur de blocs
categorystring'fieldforge'Slug de catégorie de bloc
iconstring'admin-generic'Nom de Dashicon
keywordsarray[]Mots-clés de recherche (max 3)
post_typesarray[]Restreindre à des types de publication spécifiques
modestring'preview''preview', 'edit', 'auto'
alignstring''Alignement par défaut
render_templatestring''Chemin absolu vers le modèle PHP
render_callbackcallable\nullnullFonction de rappel
enqueue_stylestring''URL du fichier CSS
enqueue_scriptstring''URL du fichier JS
enqueue_assetscallable\nullnullFonction pour l’enregistrement des ressources
supportsarray[]Prise en charge des blocs WordPress
examplearray[]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 );
    }
} );

Assistant IA Forge En ligne

Bonjour ! Je suis l'assistant IA Field Forge. Posez-moi n'importe quelle question sur le plugin — configuration, fonctionnalités, dépannage ou développement.

À l'instant
Propulsé par Forge IA · Parcourir la documentation