PHP code example of gebruederheitz / wp-gutenberg-blocks

1. Go to this page and download the library: Download gebruederheitz/wp-gutenberg-blocks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gebruederheitz / wp-gutenberg-blocks example snippets




use Gebruederheitz\GutenbergBlocks\BlockRegistrar;

BlockRegistrar::getInstance();



use Gebruederheitz\GutenbergBlocks\BlockRegistrar;

BlockRegistrar::getInstance()
    ->setScriptPath('/scripts/gutenberg.js')
    ->setScriptHandle('my-gutenberg-blocks')
;

BlockRegistrar::getInstance()->setAllowedBlocks(true);

BlockRegistrar::getInstance()->setAllowedBlocks(
    ['core/columns', 'core/column', 'core/paragraph']
);

BlockRegistrar::getInstance()->setAllowedBlocks('/my-theme/config/example.yaml');

use Gebruederheitz\GutenbergBlocks\BlockRegistrar;

function allowCustomBlock(array $allowedBlocks): array {
    $allowedBlocks[] = 'my/block';
    return $allowedBlocks;
}

add_filter(BlockRegistrar::HOOK_ALLOWED_BLOCKS, 'allowCustomBlock');

# functions.php or your block component library (or anywhere, really, but called on every request)
use Gebruederheitz\GutenbergBlocks\DynamicBlock;
use Gebruederheitz\GutenbergBlocks\BlockRegistrar;

BlockRegistrar::getInstance();

$myblock = new DynamicBlock(
    // Required: Block name needs to match the name the block was registered with in JS
    'namespace/block-name',
    // Required: Absolute path to the template partial rendering the block
    dirname(__FILE__) . '/templates/my-block.php', 
    // List of block attributes with type and default value.
    // You don't need to provide all attributes, only those that should receive
    // default values. Defaults to [].
    [                               
        'attributeName' => [
            'type' => 'string',
            'default' => 'default value',
        ],       
    ],
    // List of 

DynamicBlock::make('ghwp/example', get_template_directory() . '/template-parts/blocks/example.php')
    ->register();