How To Develop Widgets For Gutenberg Using ACF

How To Develop Widgets For Gutenberg Using ACF

1 May 2020 6 min read

Gutenberg is a new WordPress editor for posts and pages, built on the principles of builders, allows you to write custom page builder elements.

Using the ACF plugin, you can very simply make a widget of any type. And here we’ll show how.

For example, it was necessary to develop a block of selected categories.

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_1-e1588327722345

The client wanted to be able to change icons, text, title and link, insert this block on any page or post, without reference to templates or post-types, in any part of the content part of the page.

This is how the widget looks in the admin panel in a visual format

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_2

as well as in edit mode

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_3

This block is added to the page using a custom widget built on the basis of ACF plugin.

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_4

To create a block, first we need to register it in functions.php

add_action('acf/init', 'wds_acf_init');
function wds_acf_init() {

// check function exists
if( function_exists('acf_register_block') ) {

// register a testimonial block
acf_register_block(array(
'name'                => 'future_cat',
'title'                => __('Featured category'),
'description'        => __('A Featured category block.'),
'render_template'   => get_template_directory() . '/global-templates/block/content-future_cat.php',
'enqueue_script'    => get_template_directory_uri() . '/global-templates/block/future_cat.js',
'enqueue_style'    => get_template_directory_uri() . '/global-templates/block/future_cat.css',
'category'            => 'widgets',
'icon'                => 'screenoptions',
'keywords'            => array( 'category', 'featured' ),
));

}
}

Here we register the block, its files styles and scripts, as well as a template for output.

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_5

Indicate in which category to display it

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_6

and icon’s code from Dashicons(is the official icon font of the WordPress admin)

After that, in the ACF plugin, create a new group of fields, to which we connect the registered before block.

How-To-Develop-Widgets-For-Gutenberg-Using-ACF_7

Fill it with the necessary fields.

Next, in the PHP template file, display the contents of the block with a standard ACF loop.

<?php
/**
* Block Name: Future category
*
* This is the template that displays the testimonial block.
*/

// check if the repeater field has rows of data
if( have_rows('category_list') ):

?>
<div class="flex flex--wrap waffle waffle--large">

<?php
// loop through the rows of data
while ( have_rows('category_list') ) : the_row();

?>

<div class="flex__column flex__column--6 flex">

<div class="media-thumbnail">

<a class="media-thumbnail__inner flex flex--stack flex--queue@xlarge waffle waffle--large" href="<?php echo 
get_category_link(get_sub_field('block_item')); ?>">

<span class="flex__column flex__column--shrink">

<span class="media-thumbnail__icon"><?php echo file_get_contents(get_sub_field('taxonomy_icon')); ?></span>

</span>

<div class="media-thumbnail__body flex__column">

<h3 class="media-thumbnail__heading"><?php the_sub_field('block_title'); ?></h3>

<div class="media-thumbnail__content">

<?php the_sub_field('short_description'); ?>

</div>

</div>

</a>

</div>

</div>

<?php

endwhile;

?>

</div>

<?php

endif;

?>

If necessary, write styles and scripts in previously connected files.

That’s all, building blocks is pretty simple process, it’s convenient to use, and they are isolated and will always look equally good in any part of the content of a page or post.

And here in the ACF documentation, you can read more details if you need, on how the plugin works with the editor.

ERP development final cta

Get a Custom Solution with Web Design Sun

At Web Design Sun, we specialize in building web applications for clients in every business and industry.  If you’re interested in custom applications for your business, contact us today.

Contact us today to get started

More From Blog

Mobile-First Indexing in 2021: A Complete Guide

Mobile-first indexing refers to Google’s new approach to indexing and ranking content on mobile sites.
22 Mar 2021 19 min read

How to Strengthen Your Offline Business With Online Features

Are you ready to start working remotely? We're looking forward to sharing ideas and tips with our customers and anyone else who's looking for ways to ease the transition.
24 Apr 2020 6 min read

GitLab Installation Tutorial (Part2)

After GitLab is successfully configured, there is Plesk integration next. Create /etc/nginx/conf.d/gitlab.conf file with the following content
10 Apr 2016 12 min read