humanmade/extended-block-variations
Composer 安装命令:
composer require humanmade/extended-block-variations
包简介
Extends theme.json block style variations to support external stylesheets via the non-standard 'stylesheet' property.
README 文档
README
Extends WordPress theme.json block style variations with custom properties for external stylesheets.
Overview
WordPress supports defining block style variations in theme.json partials located in the /styles/ directory. However, the native implementation doesn't support linking external CSS files to style variations, either by style_handle or by file: reference.
This plugin adds support for this feature through a custom property in your theme.json variation files.
Usage
Custom Property
Add this custom property to your block style variation JSON files in themes/your-theme/styles/.../*.json:
stylesheet (string)
Path to an external stylesheet or a registered style handle.
- File references: Use
"file:./path/to/file.css"to reference a CSS file relative to the JSON file's location - Style handles: Use a string matching a registered WordPress style handle
Example
File: themes/your-theme/styles/blocks/button/primary.json
{
"$schema": "https://schemas.wp.org/trunk/theme.json",
"version": 3,
"title": "Primary",
"slug": "primary",
"blockTypes": ["core/button"],
"stylesheet": "file:./primary-button.css",
"styles": {
"color": {
"background": "var:preset|color|primary",
"text": "var:preset|color|white"
}
}
}
File: themes/your-theme/styles/blocks/button/primary-button.css
.is-style-primary { /* Additional styles requiring media queries or other CSS-only features */ @media (max-width: 768px) { padding: 1em 2em; } }
File Path Resolution
File paths are resolved relative to the JSON file's directory:
- JSON at:
themes/your-theme/styles/blocks/button/primary.json - Reference:
"stylesheet": "file:./styles.css" - Resolves to:
themes/your-theme/styles/blocks/button/styles.css
Use ../ to reference parent directories:
- Reference:
"stylesheet": "file:../../shared/button-base.css" - Resolves to:
themes/your-theme/styles/shared/button-base.css
Using Pre-registered Handles
If you've already registered a style handle via wp_register_style(), reference it directly:
{
"stylesheet": "my-custom-handle"
}
Versioning stylesheets
WordPress normally sets block asset versions based on the version string in block.json. Since the schema for block style variation JSON partials does not include a version number, you can manually specify a version string in your stylesheet reference:
"stylesheet": "file:/variation-styles.css?ver=1.0.2"
If you omit the ver parameter, a version string will be computed by hashing the stylesheet. This requires an inefficient full-file read operation on every registration call, so this auto-versioning should only be used as a fallback convenience while actively developing styles.
How It Works
- WordPress core reads theme.json partials from
/styles/and registers block style variations - This plugin scans the same directory for JSON files containing a
stylesheetproperty - For
file:references:- The path is resolved relative to the JSON file's location
- A unique style handle is generated based on the file path
- The stylesheet is registered with WordPress (including RTL support and path data for inlining)
- Stylesheets are enqueued via
wp_enqueue_block_style():- If block assets load on-demand: Enqueued when blocks with the variation class render
- Otherwise: Enqueued immediately
- WordPress handles merging the styles with theme.json definitions and potential inlining
Compatibility
- WordPress: 6.6+
- PHP: 8.0+
This plugin extends core WordPress functionality and follows the same file structure conventions as WordPress core's theme.json parser.
API Reference
Extended_Block_Variations\register_variation_stylesheets(): void
Enqueues stylesheets for block style variations that define a custom stylesheet property. Handles both immediate and on-demand loading based on theme configuration.
Performance Optimizations
The plugin integrates with WordPress's block asset loading strategies:
- On-demand loading: When enabled, stylesheets only load when blocks with the variation are actually rendered
- Automatic inlining: Small stylesheets (under 40KB by default) may be inlined automatically
- RTL support: Automatically detects and loads RTL versions of stylesheets when available
- File versioning: Uses file modification time for cache busting
Integration with Theme
WordPress core automatically registers block style variations from theme.json partials in the /styles/ directory. This plugin enhances that process by:
- Detecting variations with custom
stylesheetproperties - Resolving and registering the referenced stylesheets
- Enqueueing them alongside the core-registered block styles
The plugin hooks into after_setup_theme, running before block registration to ensure stylesheets are ready when blocks render. No additional configuration is required - simply add the stylesheet property to your variation JSON files.
Setting Default Block Styles
To configure default block styling, use theme.json to set styles at the block level rather than the variation level. This is the WordPress-native approach and ensures proper inheritance and overrides.
humanmade/extended-block-variations 适用场景与选型建议
humanmade/extended-block-variations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 689 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「wordpress」 「theme.json」 「block-variations」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 humanmade/extended-block-variations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 humanmade/extended-block-variations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 humanmade/extended-block-variations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Plugin for YOURLS. Default tools to use in some laemmi plugins
WordPress theme.json generator from a PHP array
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Allows polling user for event dates and times.
Support swipe touch gestures to navigate in paginated lists and on Tidypics image pages.
统计信息
- 总下载量: 689
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2026-04-21