Skip to content

Repository files navigation

ThemePlate Blocks

Usage

useThemePlate\Blocks\BlockType;
/** https://developer.wordpress.org/reference/classes/wp_block_type/__construct/#parameters */$config = array(
'namespace' => 'my-blocks',
'template' => '/path/to/render.php',
);
( newBlockType( 'Custom Block', $config ) )->fields( $list )->init();
/** >= 1.6.0 */
( newBlockType( __DIR__ . '/tests/example' ) )->init()

Check out example block

Restrict inner blocks and prefill components

$config = array(
'allowed_blocks' => array(
'core/image',
'core/heading',
'core/paragraph',
),
'template_blocks' => array(
array( 'core/image', array() ),
array( 'core/heading', array( 'placeholder' => 'Insert title here' ) ),
array( 'core/paragraph', array( 'placeholder' => 'Insert content copy' ) ),
),
);
( newBlockType( 'My custom block', $config ) )->fields( $list )->init();
/** >= 1.6.0 */// return in the config.php file beside block.jsonreturn$config;

Disable nested blocks by setting $config key inner_blocks to false

Structured (Bulk) Definition

/path/to/blocks/
├── first-block/
│ ├── block.json // >= 1.6.0
│ ├── config.php
│ └── markup.php
├── second-block/
├── block.json // >= 1.6.0
├── config.php
└── markup.php
useThemePlate\Blocks\CustomBlocks;
( newCustomBlocks( 'My Blocks', '/path/to/blocks' ) )->init();
/** >= 1.6.0 */
( newCustomBlocks( '/path/to/blocks' ) )->init()

*/block/block.json

{
...https://developer.wordpress.org/block-editor/reference-guides/block-api/block-metadata/...
}

*/block/config.php

<?phpuseThemePlate\Blocks\BlockType;
return ( newBlockType( 'My custom block' ) )->fields( $list );
/** >= 1.6.0 */returnarray(
...
/** https://developer.wordpress.org/reference/classes/wp_block_type/__construct/#parameters */...
);

*/block/markup.php

<?php/** * @var array $attributes Block attributes. * @var string $content Block inner content. * @var WP_Block $block Block instance. */// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped?>
<div <?phpechoget_block_wrapper_attributes(); ?>>
<?phpecho$content; ?>
<pre><?phpprint_r( $attributes ); ?></pre>
<pre><?phpprint_r( $block ); ?></pre>
</div>

About

Easy server-side-rendered blocks

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages