wp63 / acf-block-loader
Wrapper class for easy ACF Gutenberg Block registration
Installs: 7 870
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 1
Forks: 2
Open Issues: 0
Type:package
pkg:composer/wp63/acf-block-loader
Requires
- php: >=7.1
This package is auto-updated.
Last update: 2025-10-25 23:32:21 UTC
README
Wrapper class for easy ACF Gutenberg Block registration
Install
composer require wp63/acf-block-loader
Usage
By default, Class file are to be stored inside Blocks directory in theme root directory (or app\Blocks in Sage 9). File name should be the same as Classname as in PSR-4.
- Create a class extending
WP63\Block - Class default namespace is
WP63\Blocks\ - Class must have at least 2 methods:
register()and staticrender()
<?php namespace App\Blocks; use WP63\Block; class MyBlock extends Block { protected function register() { return [ 'name' => $name, 'title' => $title, 'category' => $category, 'fields' => $fields, ]; } public static function render( $options ) { // You can directly render HTML within `render()` method ... display block html ... // Or alternatively, return an array for Blade template engine in Sage 9 return [ 'name' => 'value', // $name in template file 'foo' => 'bar', // $foo in template file ]; } }
register() method will return an array with 3 keys
$nameblock-unique-name$titleBlock Title$categoryBlock category. Predefined categories are [ common | formatting | layout | widgets | embed ] (Optional)$fieldsACF fieldsStoutLogic\AcfBuilder\FieldsBuilderobject. Output ofFieldsBuilder::build()method.
render() is the static method for rendering actual block. Every output generated inside this method will be part of block html. Render method will have access to 4 callback arguments from ACF via $options arguments
- array
$options->blockThe block settings and attributes. - string
$options->contentThe block inner HTML (empty). - boolean
$options->is_previewTrue during AJAX preview. - int|string
$options->post_idThe post ID this block is saved to.
Filters
wp63/acf_block_namespaceChange block namespace. Default:App\Blocks\wp63/acf_block_directoryChange directory name. Default:./Blockswp63/acf_block_template_directoryChange Blade template directory. relative to/viewsdirectory Default:blocks
Actions
wp63/before_block_renderBefore each block is renderedwp63/before_block_render/{$block_name}Before$block_nameis renderedwp63/after_block_renderAfter each block is renderedwp63/after_block_render/{$block_name}After$block_nameis rendered
Use with Blade template engine in Sage 9
- In class
render()method, instead of echo output directly, return an array contains all data to be exposed to Blade template file. - Create template file in
views/blocksthe file name must be the same as block nam3 ($name). for example:views/blocks/hero-banner.blade.php