Skip to content

Instantly share code, notes, and snippets.

@miziomon
Created May 21, 2026 20:32
Show Gist options
  • Select an option

  • Save miziomon/716fd8154ab48a3d9947568cad8b7801 to your computer and use it in GitHub Desktop.

Select an option

Save miziomon/716fd8154ab48a3d9947568cad8b7801 to your computer and use it in GitHub Desktop.
blocco gutenbergo solo php
add_action( 'init', function () {
// Registriamo un blocco interamente lato server. Niente block.json, niente JS.
register_block_type( 'mavida/box-avviso', array(
'title' => 'Box avviso',
'category' => 'widgets',
'icon' => 'info',
// Gli attributi definiti qui generano da soli i controlli nell'inspector:
// una stringa diventa un campo di testo, un enum diventa un menu a tendina.
'attributes' => array(
'messaggio' => array(
'type' => 'string',
'default' => 'Scrivi qui il tuo avviso.',
),
'tipo' => array(
'type' => 'string',
'enum' => array( 'info', 'attenzione', 'successo' ),
'default' => 'info',
),
),
// Il flag che attiva la registrazione in solo PHP e la UI automatica.
// Abilitiamo anche i controlli nativi per i colori di sfondo e testo.
'supports' => array(
'autoRegister' => true,
'color' => array(
'background' => true,
'text' => true,
),
),
// Il render gira sia nell'editor sia nel frontend: ciò che vedi è ciò che esce.
'render_callback' => function ( $attributes ) {
// get_block_wrapper_attributes() porta in output le classi e gli stili
// generati dai supports (es. i colori), altrimenti non comparirebbero davanti.
$wrapper = get_block_wrapper_attributes( array(
'class' => 'box-avviso box-avviso--' . esc_attr( $attributes['tipo'] ),
) );
// Escape sempre dell'output, perché i dati arrivano dall'editor.
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper,
esc_html( $attributes['messaggio'] )
);
},
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment