Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created December 28, 2022 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thierrypigot/9cc56595ca9454bfea6945d264037360 to your computer and use it in GitHub Desktop.
Save thierrypigot/9cc56595ca9454bfea6945d264037360 to your computer and use it in GitHub Desktop.
Custom Gutenberg bloc
<?php
// Register the hero block
function register_hero_block() {
register_block_type('my-plugin/hero', array(
'render_callback' => 'render_hero_block',
'attributes' => array(
'title' => array(
'type' => 'string',
'default' => '',
),
'text' => array(
'type' => 'string',
'default' => '',
),
'buttonText' => array(
'type' => 'string',
'default' => '',
),
'buttonUrl' => array(
'type' => 'string',
'default' => '',
),
),
));
}
// Render the hero block
function render_hero_block($attributes) {
$title = $attributes['title'];
$text = $attributes['text'];
$buttonText = $attributes['buttonText'];
$buttonUrl = $attributes['buttonUrl'];
ob_start();
?>
<div class="hero-section">
<h1><?php echo $title; ?></h1>
<p><?php echo $text; ?></p>
<?php if($buttonText && $buttonUrl) : ?>
<a href="<?php echo $buttonUrl; ?>" class="button"><?php echo $buttonText; ?></a>
<?php endif; ?>
</div>
<?php
return ob_get_clean();
}
// Hook the block registration into the init action
add_action('init', 'register_hero_block');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment