offset/blocks

更轻松地创建 Gutenberg 区块

1.0.1 2021-12-30 09:08 UTC

This package is not auto-updated.

Last update: 2024-09-25 02:11:45 UTC


README

安装

composer require offset/blocks

使用

use 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();

您也可以使用函数来渲染内容

// 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>';
});

过滤器和使用者覆盖

过滤器会自动添加到您的区块中,以在开发过程中提供更大的灵活性和定制性。

offset_block_attributes

过滤所有 Gutenberg 区块的属性。

参数
  • $attributes - array - 区块属性。默认为空数组
示例
function attributes_filter($attributes) {
    $attributes['myValue'] = 'Changed';
    return $attributes;
}

add_filter('offset_block_attributes', 'attributes_filter', 10, 1);

offset_block_attributes_{block_name}

过滤具有相同名称的 Gutenberg 区块的属性。

block_name 对应于 block.json 文件中的 name 参数,将特殊字符替换为 _(例如:offset-pack/block-one -> offset_pack_block_one)。

参数
  • $attributes - array - 区块属性。默认为空数组
示例
function attributes_filter($attributes) {
    $attributes['myValue'] = 'Changed';
    return $attributes;
}

add_filter('offset_block_attributes_offset_pack_block_one', 'attributes_filter', 10, 1);

offset_block_content

过滤所有 Gutenberg 区块的内容。

参数
  • $content - string - 区块内容。默认为空字符串。
示例
function content_filter($content) {
    $content .= 'Add new line';
    return $content;
}

add_filter('offset_block_content', 'content_filter', 10, 1);

offset_block_content_{block_name}

过滤具有相同名称的 Gutenberg 区块的内容。

block_name 对应于 block.json 文件中的 name 参数,将特殊字符替换为 _(例如:offset-pack/block-one -> offset_pack_block_one)。

参数
  • $content - string - 区块内容。默认为空字符串。
示例
function content_filter($content) {
    $content .= 'Add new line';
    return $content;
}

add_filter('offset_block_content_offset_pack_block_one', 'content_filter', 10, 1);

offset_block_render_{block_name}

过滤具有相同名称的 Gutenberg 区块的 HTML。

block_name 对应于 block.json 文件中的 name 参数,将特殊字符替换为 _(例如:offset-pack/block-one -> offset_pack_block_one)。

参数
  • $html - string - 区块 HTML。默认为空字符串
  • $attributes - array - 区块属性。默认为空数组
  • $content - 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);

offset_block_is_style_enqueue

从所有 Gutenberg 区块中移除样式。

示例

add_filter('offset_block_is_style_enqueue', '__return_false');

offset_block_is_style_enqueue_{block_name}

从具有相同名称的 Gutenberg 区块中移除样式。

block_name 对应于 block.json 文件中的 name 参数,将特殊字符替换为 _(例如:offset-pack/block-one -> offset_pack_block_one)。

示例

add_filter('offset_block_is_style_enqueue_offset_pack_block_one', '__return_false');

offset_block_is_script_enqueue

从所有 Gutenberg 区块中移除脚本。

示例

add_filter('offset_block_is_script_enqueue', '__return_false');

offset_block_is_script_enqueue_{block_name}

从具有相同名称的 Gutenberg 区块中移除脚本。

block_name 对应于 block.json 文件中的 name 参数,将特殊字符替换为 _(例如:offset-pack/block-one -> offset_pack_block_one)。

示例

add_filter('offset_block_is_script_enqueue_offset_pack_block_one', '__return_false');