Hi,
with Gutenberg and the new Block Registration (ACF Beta 3) i ask my self, how to build websites in the future.
Currently im thinking on something like these.
Template
– functions.php
– main.scss
— Template Folder
— Template.php
— Style.scss
The functions.php includes the template File
The main.scss includes the style.css
Inside the template file i do
– register block
– create custom fields (php – my_acf_add_local_field_groups)
– the rendered html
So i can create different templates very easily and can add them easily to every template. just copy/paste the template folder and icclude it.
What are your thoughts?
Heres the code
<?php
add_action('acf/init', 'my_register_blocks');
function my_register_blocks() {
// check function exists
if( function_exists('acf_register_block') ) {
// register a testimonial block
acf_register_block(array(
'name' => 'testimonial',
'title' => __('Testimonial'),
'description' => __('A custom testimonial block.'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'formatting',
'icon' => 'admin-comments',
'mode' => 'preview',
'keywords' => array( 'testimonial', 'quote' ),
));
}
}
function my_acf_add_local_field_groups() {
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'One Column Text Gutenberg',
'fields' => array(
array(
'key' => 'field_5c0a4fd48b754',
'label' => 'Headline',
'name' => 'acf_gutenberg_onetextcolum_headline',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'block',
'operator' => '==',
'value' => 'acf/testimonial',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => 1,
'description' => '',
));
}
add_action('acf/init', 'my_acf_add_local_field_groups');
function my_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
/* print_r($block);
print_r($content); */
$headlinegt = get_field('acf_gutenberg_onetextcolum_headline');
?>
<section class="tp-text-onecolumn-gutenberg">
<div class="container">
<div class="row">
<div class="col-12">
<div class="headline-container">
<h2 class="headline"><?php echo $headlinegt;?></h2>
</div>
</div>
</div>
</div>
</section>
<?php
};
?>
Hi @enky
This is a good idea. I like the modular approach as this keeps your code clean!
This thread will be super useful to other developers who start using ACF Blocks in their themes, so please keep updating it as you discover more.
This is great @enky,
I do have one question, I am not seeing ‘enqueue_style’ anywhere, I assume it would just be added to the register block array?
TIA
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.