AgentSkillsCN

native-block-development

Oh My Brand! FSE主题中,创建原生WordPress区块的指南。涵盖区块结构、block.json配置、render.php模板以及资源注册。在构建新区块时,请使用本指南。

SKILL.md
--- frontmatter
name: native-block-development
description: Guide for creating native WordPress blocks in Oh My Brand! FSE theme. Block structure, block.json configuration, render.php templates, and asset registration. Use when building new blocks.
metadata:
  author: Wesley Smits
  version: "1.0.0"

Native Block Development

Creating native WordPress blocks for the Oh My Brand! FSE theme using @wordpress/scripts.


When to Use

  • Creating new WordPress blocks with editor and frontend components
  • Configuring block metadata and supports
  • Writing PHP render templates for server-side rendering
  • Registering block assets (styles, scripts)

Block File Structure

Every block lives in src/blocks/{block-name}/:

code
src/blocks/gallery/
├── block.json          # Block metadata and registration
├── index.js            # Block registration entry point
├── edit.tsx            # Editor React component
├── render.php          # Server-side render template
├── helpers.php         # Block-specific helper functions
├── style.css           # Frontend styles (auto-enqueued)
├── editor.css          # Editor-only styles (optional)
├── view.ts             # Frontend TypeScript (Web Component)
└── view.test.ts        # Unit tests

File Templates

Use templates from block-scaffolds:

FileTemplatePurpose
block.jsonblock-json-native.jsonBlock metadata
render.phprender-native.phpServer-side render
helpers.phphelpers-native.phpHelper functions
view.tsview.tsFrontend Web Component
style.cssstyle.cssFrontend styles
edit.tsxedit.tsxEditor component

block.json Key Properties

PropertyRequiredDescription
$schemaYeshttps://schemas.wp.org/trunk/block.json
apiVersionYesAlways use 3
nameYesFormat: theme-oh-my-brand/{block-name}
versionYesSemantic version (e.g., 1.0.0)
textdomainYestheme-oh-my-brand
renderYesfile:./render.php
editorScriptYesfile:./index.js
viewScriptOptionalfile:./view.js for frontend JS
styleYesfile:./style.css

Common Supports

json
"supports": {
    "align": ["wide", "full"],
    "anchor": true,
    "className": true,
    "color": {
        "background": true,
        "text": true
    },
    "spacing": {
        "margin": true,
        "padding": true,
        "blockGap": true
    }
}

Render Template Guidelines

GuidelineDescription
declare(strict_types=1)Always include at top
Early returnReturn early if no data to display
get_block_wrapper_attributes()Use for consistent wrapper
Escape all outputesc_html(), esc_attr(), esc_url()
Unique IDsUse wp_unique_id() for instances
Web Component wrapperUse <omb-{block-name}> for frontend JS

Helper Function Guidelines

GuidelineDescription
Function prefixUse omb_{block_name}_ prefix
function_exists() guardWrap in existence check
Type hintsUse parameter and return types
DocBlocksInclude @param and @return

Asset Handling

Automatic Loading

Assets defined in block.json are automatically enqueued:

PropertyWhen Loaded
styleFrontend and editor
editorStyleEditor only
editorScriptEditor only
viewScriptFrontend only

Assets only load when the block is present on the page.


Step-by-Step Creation

  1. Create directory: mkdir -p src/blocks/my-block
  2. Add block.json: Copy from template, replace placeholders
  3. Create edit.tsx: See block-editor-components
  4. Create render.php: Copy from template
  5. Create helpers.php: Copy from template
  6. Create view.ts: See web-components
  7. Add style.css: See css-standards
  8. Write tests: See vitest-testing
  9. Build: pnpm run build
  10. Verify: Check block appears in editor

Related Skills


References