AgentSkillsCN

acf-block-registration

Oh My Brand! FSE主题中,使用字段组创建ACF PRO区块的指南。在注册ACF区块、创建字段组,或处理ACF专属渲染模板时,请使用本指南。

SKILL.md
--- frontmatter
name: acf-block-registration
description: Guide for creating ACF PRO blocks with field groups in Oh My Brand! FSE theme. Use this when registering ACF blocks, creating field groups, or working with ACF-specific render templates.
metadata:
  author: Wesley Smits
  version: "1.0.0"

ACF Block Registration

Creating ACF PRO blocks with custom field groups for the Oh My Brand! FSE theme.


When to Use

  • Creating blocks that use ACF field groups for content entry
  • Building blocks with repeater fields, galleries, or complex field layouts
  • Leveraging ACF PRO features (repeaters, flexible content, clone fields)
  • Rapid block prototyping with ACF's visual field editor

ACF vs Native Blocks

AspectACF BlockNative Block
Schemahttps://advancedcustomfields.com/schemas/json/main/block.jsonhttps://schemas.wp.org/trunk/block.json
Name prefixacf/theme-oh-my-brand/
API Version23
AttributesDefined in ACF field groupsDefined in block.json
Data accessget_field()$attributes array
Render propertyacf.renderTemplaterender
Editor experienceACF field formsCustom React components
Best forContent-heavy blocks, rapid prototypingComplex interactivity, custom UI

Block File Structure

ACF blocks live in blocks/acf-{block-name}/:

code
blocks/acf-faq/
├── block.json          # ACF block metadata (ACF schema)
├── render.php          # Render template (referenced in block.json)
├── helpers.php         # Block-specific helper functions
├── style.css           # Frontend styles (auto-enqueued)
└── editor.css          # Editor-only styles (optional)

Field groups are stored separately in acf-json/.


File Templates

Use templates from block-scaffolds:

FileTemplatePurpose
block.jsonblock-json-acf.jsonACF block metadata
render.phprender-acf.phpRender template
helpers.phphelpers-acf.phpHelper functions
Field groupfield-group-scaffold.jsonACF field group

Full Examples

ExamplePurpose
render-faq-example.phpComplete FAQ render with JSON-LD
helpers-faq-example.phpComplete FAQ helpers
block-registration.phpPHP registration function
acf-json-sync.phpACF local JSON config

block.json Key Properties (ACF)

PropertyRequiredDescription
$schemaYeshttps://advancedcustomfields.com/schemas/json/main/block.json
nameYesFormat: acf/{block-name}
acf.modeYespreview, edit, or auto
acf.renderTemplateYesPath to render PHP file
styleOptionalfile:./style.css

ACF Mode Options

ModeDescription
previewShows rendered block output, click to edit fields
editShows ACF field form directly
autoSwitches between preview and edit automatically

Field Group Structure

Field groups connect to blocks via location rules:

json
"location": [
    [
        {
            "param": "block",
            "operator": "==",
            "value": "acf/faq"
        }
    ]
]

Title Conventions

TypeTitle FormatExample
Block field groupsBlock: {Block Name}Block: FAQ
Options pages{Page Name} SettingsTheme Settings
Post type fields{Post Type} FieldsCompany Fields

Render Template Guidelines

GuidelineDescription
declare(strict_types=1)Always include at top
Helper fileUse require_once __DIR__ . '/helpers.php'
Data retrievalUse helper functions, not inline get_field()
Empty state checkReturn early if no data (except in editor)
get_block_wrapper_attributes()Use for consistent wrapper
Editor placeholderShow message when data is empty in editor

Template Variables

VariableTypeDescription
$blockarrayBlock settings (id, name, className, align)
$contentstringInner HTML (usually empty for ACF blocks)
$is_previewboolTrue when rendering in editor preview
$post_idintPost ID where block is used
$contextarrayBlock context values

Helper Function Guidelines

GuidelineDescription
function_exists() guardWrap functions in existence check
Default valuesAlways provide defaults with ?: [] or ?? ''
Type hintsUse parameter and return type declarations
Data transformationNormalize ACF field names to consistent keys
ACF guardCheck function_exists('get_field')

Common Field Types

Repeater Field

php
$items = get_field('repeater_field_name') ?: [];

foreach ($items as $item) {
    $sub_value = $item['sub_field_name'] ?? '';
}

Gallery Field

php
$images = get_field('gallery') ?: [];

foreach ($images as $image) {
    $url = $image['url'] ?? '';
    $alt = $image['alt'] ?? '';
}

Image Field

php
$image = get_field('image');

if ($image) {
    $url = $image['url'];
    $alt = $image['alt'];
}

Options Page Fields

php
$logo = get_field('site_logo', 'option');

Step-by-Step Creation

  1. Create directory: mkdir -p blocks/acf-my-block
  2. Add block.json: Copy from template, use ACF schema
  3. Create field group: In WP Admin > ACF > Field Groups
    • Title: Block: {Block Name}
    • Location: Block equals acf/{block-name}
  4. Create render.php: Copy from template
  5. Create helpers.php: Copy from template
  6. Add style.css: See css-standards
  7. Register block: Add to $acf_blocks array in functions.php
  8. Verify: Block appears in editor and renders correctly

Or use the scaffolding script:

bash
.github/skills/block-scaffolds/scripts/create-block.sh acf my-block "My Block Title"

Related Skills


References