← スキル一覧に戻る

gutenberg-blocks
by michalstaniecko
⭐ 0🍴 0📅 2026年1月24日
SKILL.md
name: gutenberg-blocks description: Use this skill when creating or modifying Gutenberg blocks with ACF. Covers block.json configuration, render templates, block registration, and JavaScript interactivity.
Gutenberg Blocks Development
Overview
This project uses two methods for creating Gutenberg blocks:
- Native ACF blocks - Using
block.jsonwith ACF mode (preferred) - Legacy ACF blocks - Using
acf_register_block()function
Key Files
blocks/- Native block directories with block.jsontemplate-blocks/- Render templates for legacy blocksinc/acf-blocks.php- Block registrationacf-fields/- ACF field group JSON files
Method 1: Native ACF Blocks (Preferred)
Directory Structure
blocks/block-name/
├── block.json # Block definition
├── block-name.php # Render template
├── block-name.css # Block styles (optional)
└── block-name.js # Block interactivity (optional)
block.json Example
{
"name": "sardynkibiznesu20/block-name",
"title": "Block Title",
"description": "Block description.",
"style": ["file:./block-name.css"],
"category": "text",
"icon": "admin-comments",
"keywords": ["keyword1", "keyword2"],
"acf": {
"mode": "edit",
"renderTemplate": "block-name.php"
},
"script": "file:./block-name.js"
}
Block Registration
Register in inc/acf-blocks.php:
function sb_register_acf_blocks() {
register_block_type(
untrailingslashit(get_stylesheet_directory()) . '/blocks/block-name'
);
}
add_action('init', 'sb_register_acf_blocks');
Render Template (block-name.php)
<?php
$questions = get_field('questions');
$block_id = 'block-' . $block['id'];
$class_name = 'sb-block-name';
if (!empty($block['className'])) {
$class_name .= ' ' . $block['className'];
}
?>
<div id="<?php echo esc_attr($block_id); ?>" class="<?php echo esc_attr($class_name); ?>">
<?php if ($questions): ?>
<?php foreach ($questions as $question): ?>
<div class="item">
<h3><?php echo esc_html($question['title']); ?></h3>
<p><?php echo wp_kses_post($question['content']); ?></p>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
Method 2: Legacy ACF Blocks
Registration with acf_register_block()
add_action('acf/init', 'sb_acf_init');
function sb_acf_init() {
if (function_exists('acf_register_block')) {
acf_register_block(array(
'name' => 'sb-block-name',
'title' => __('Block Title'),
'description' => __('Block description.'),
'render_callback' => 'sb_acf_block_render_callback',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array('keyword1', 'keyword2'),
));
}
}
Render Callback
function sb_acf_block_render_callback($block, $content, $is_preview) {
$slug = str_replace('acf/', '', $block['name']);
$category = $block['category'];
if (file_exists(get_theme_file_path("/template-blocks/{$category}-{$slug}.php"))) {
include(get_theme_file_path("/template-blocks/{$category}-{$slug}.php"));
}
}
Template File Naming
Template files follow pattern: template-blocks/{category}-{slug}.php
Example: template-blocks/formatting-sb-button.php
JavaScript Interactivity
// blocks/block-name/block-name.js
document.addEventListener('DOMContentLoaded', function() {
const blocks = document.querySelectorAll('.sb-block-name');
blocks.forEach(function(block) {
// Initialize block functionality
const items = block.querySelectorAll('.item');
items.forEach(function(item) {
item.addEventListener('click', function() {
this.classList.toggle('active');
});
});
});
});
Best Practices
- Use native blocks - Prefer
block.jsonmethod for new blocks - Block naming - Use
sardynkibiznesu20/namespace prefix - ACF fields - Create field group in ACF and export to
acf-fields/ - Escape output - Use
esc_html(),esc_attr(),wp_kses_post() - Block ID - Use
$block['id']for unique identifiers - Inline CSS/JS - Keep styles and scripts in separate files
スコア
総合スコア
40/100
リポジトリの品質指標に基づく評価
✓SKILL.md
SKILL.mdファイルが含まれている
+20
○LICENSE
ライセンスが設定されている
0/10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
レビュー
💬
レビュー機能は近日公開予定です