A simple custom block delivered in a plugin.
On Facebook Carrie Dils asked members whether they would be interested in a tutorial on using Genesis Custom Blocks plugin. I decided to try use it to create a simple block.
Last year I used Advanced Custom Fields Pro to create a Flip Card block. The initial part where you set up the fields is very similar.
For an accordion block I just have a title field and a content field. The content field is not a WYSIWYG field as that is only available in the Pro version of the plugin.
I set an icon and a category and published the block.
I then needed to create a file in the theme: blocks/block-accordion-block.php. I kept this very simple.
Using the block
When writing a page or post you can choose the block like any other. You will be presented with the block’s fields.
The blocks look like they will on the front end because they have default browser styling. For more complicated custom blocks you can provide a preview template file (preview-accordion-block.php).
On the front end
This is how the blocks are displayed on the front end. I have clicked the first block title to expand it.
In a plugin folder
The Genesis Custom Blocks plugin expects that the block template file to be in a subdirectory of the theme. To make it easier to distribute the block code I chose to put it in a plugin. For this to work I needed to tell the plugin the new location of the template file. I used the ‘genesis_custom_blocks_template_path’ filter for this.
It only changes the template path for the Accordion block.
My plugin has 2 files:
Multiple template paths
In response to Carrie’s comment I wrote another quick plugin and I put its template file in the subdirectory. The block is called “Title Subtitle block” with 2 text fields, Title and Subtitle.
I tweaked the plugin to only change the template path for ‘blocks/block-title-subtitle-block.php‘
When I view all blocks I see the two different template paths.
Like with ACF Pro the world is now your oyster. Your block template file can be simple or quite complicated e.g. generating different markup based on field settings.