composer require offset/blocksuse Offset\Block;
// Create an empty block
$block = new Block();
// Pass the "block.json" file of your Gutenberg block
$block->setSettingsFromJSONPath(__DIR__ . '/block.json');
// Add the file that will be used for the dynamic rendering of your block. You have access to `$attributes` and `$content`.
$block->setRender(__DIR__ . '/template/view.php');
// Save your block in Gutenberg and load the styles and scripts
$block->init();You can also render the content with a function
// Add the file that will be used for the dynamic rendering of your block. You have access to `$attributes` and `$content`.
$block->setRender(function($attributes, $content) {
return '<div>My block</div>';
});Filters are automatically added to your blocks to give you greater flexibility in development and customization.
Filter all attributes of all Gutenberg blocks.
$attributes-array- Block attributes. Default empty array
function attributes_filter($attributes) {
$attributes['myValue'] = 'Changed';
return $attributes;
}
add_filter('offset_block_attributes', 'attributes_filter', 10, 1);Filter the attributes of Gutenberg blocks that have the same name.
The block_name corresponds to the name parameter in the block.json file, replacing special characters with _ (example: offset-pack/block-one -> offset_pack_block_one).
$attributes-array- Block attributes. Default empty array
function attributes_filter($attributes) {
$attributes['myValue'] = 'Changed';
return $attributes;
}
add_filter('offset_block_attributes_offset_pack_block_one', 'attributes_filter', 10, 1);Filters all contents of all Gutenberg blocks.
$content-string- Block content. Default empty string.
function content_filter($content) {
$content .= 'Add new line';
return $content;
}
add_filter('offset_block_content', 'content_filter', 10, 1);Filter the content of Gutenberg blocks that have the same name.
The block_name corresponds to the name parameter in the block.json file, replacing special characters with _ (example: offset-pack/block-one -> offset_pack_block_one).
$content-string- Block content. Default empty string.
function content_filter($content) {
$content .= 'Add new line';
return $content;
}
add_filter('offset_block_content_offset_pack_block_one', 'content_filter', 10, 1);Filter the html of Gutenberg blocks that have the same name.
The block_name corresponds to the name parameter in the block.json file, replacing special characters with _ (example: offset-pack/block-one -> offset_pack_block_one).
$html-string- Block html. Default empty string$attributes-array- Block attributes. Default empty array$content-string- Block content. Default empty string.
function render_filter($html, $attributes, $content) {
$html .= '<div>Add new block</div>';
return $html;
}
add_filter('offset_block_render_offset_pack_block_one', 'render_filter', 10, 3);Remove styles from all Gutenberg blocks.
add_filter('offset_block_is_style_enqueue', '__return_false');Remove styles from Gutenberg blocks with the same name.
The block_name corresponds to the name parameter in the block.json file, replacing special characters with _ (example: offset-pack/block-one -> offset_pack_block_one).
add_filter('offset_block_is_style_enqueue_offset_pack_block_one', '__return_false');Remove scripts from all Gutenberg blocks.
add_filter('offset_block_is_script_enqueue', '__return_false');Remove scripts from Gutenberg blocks with the same name.
The block_name corresponds to the name parameter in the block.json file, replacing special characters with _ (example: offset-pack/block-one -> offset_pack_block_one).
add_filter('offset_block_is_script_enqueue_offset_pack_block_one', '__return_false');