Skip to content

Instantly share code, notes, and snippets.

@amielucha
Last active April 16, 2019 16:10
Show Gist options
  • Save amielucha/dd10067958fb50996e8ea37d2b891ddc to your computer and use it in GitHub Desktop.
Save amielucha/dd10067958fb50996e8ea37d2b891ddc to your computer and use it in GitHub Desktop.
WordPress Gutenberg and ACF - add a custom hero panel block
<?php
/**
* Register ACF Gutenberg Block
*/
add_action('acf/init', 'gutenberg_hero_panel_block');
function gutenberg_hero_panel_block()
{
// check function exists
if (function_exists('acf_register_block')) {
// register a testimonial block
acf_register_block(array(
'name' => 'hero_panel',
'title' => __('Hero Panel'),
'description' => __('Displays a text container on a full-width image background.'),
'render_callback' => 'gutenberg_hero_panel_callback',
'category' => 'lightseek-blocks', // Note: replace with one of the standard block categories if no custom category specified.
'icon' => 'align-left', // https://developer.wordpress.org/resource/dashicons
'keywords' => array('lightseek', 'iseek', 'custom'),
));
}
}
function gutenberg_hero_panel_callback($block)
{
$bg = get_field('hero-background');
$content = get_field('hero-content');
$align_right = get_field('align_container');
$title = $content['title'];
$text = $content['content'];
$btn_url = $content['button']['link']['url'];
$btn_label = $content['button']['label'];
?>
<section class="container-hero cover align<?php echo $block['align'] ?> <?php echo $block['className'] ?>" style="<?php echo $bg ? "background-image: url(" . $bg['sizes']['large'] . ")" : "" ?>">
<div class="container-hero-wrapper container">
<div class="hero-inner" <?php echo $align_right ? 'style="margin-left: auto;"' : '' ?> >
<?php
echo $title ? '<h1>' . $title . '</h1>' : '';
echo $text ? '<div class="text-container">' . $text . '</div>' : '';
echo $btn_url && $btn_label ? '<div class="wp-block-button alignright"><a class="wp-block-button__link" href="'.$btn_url.'">' . $btn_label . '</a></div>' : '';
?>
</div>
</div>
</section>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment