Example Gutenberg Testimonial block using Advanced Custom Fields Pro to showcase how to build a block. For demo at the Denver WordPress Developer Meetup on Nov 14, 2018
This is an example Testimonial Gutenberg Block using Advanced Custom Fields Pro's Gutenberg features.
Note: You must have ACF Pro installed in order for this block to work.
This block is for demonstration purposes and was created to show at the Denver WordPress Developer Meetup on November 14, 2018.
The commits are broken into steps. You can view the diffs and the commit messages to read about how the block was created step-by-step.
In this step we:
acf/init
to ensure Advanced Custom Fields has been initializedacf_register_block
render_callback
which is how Gutenberg renders dynamic blocks.
acf_testimonial_callback
, and inside that function we output some simple markup.At this point, we can now add the ACF Testimonial Block
in Gutenberg, and we will see the name of the block rendered in the Editor and in our Theme when the post is saved.
Block Markup and Styles - DIFF
In this step we:
$testimonial
, $alignment
, $avatar_url
, $name
, $text_color
and $background_color
.
<style>
tag along with our block markup, and we use some of our variables to make the styles dynamic.At this point, we can see our Block taking shape, both in the Gutenberg UI and in our Theme when we save a post and look at it.
Register & Connect Fields - DIFF
In this step we:
acf_testimonial_callback()
function, replacing our hard-coded variable values.Now, we have a fully interactive block with fields we can edit interactively and we see live previews of our changes, and our theme renders the dynamic values in our markup and styles.
Neat!