Method_CSS_Collector
Overview
Singleton that collects CSS from block render callbacks and outputs a single consolidated <style> tag.
Description
Blocks in Method generate scoped CSS during their PHP render callbacks. Rather than each block outputting its own <style> tag, all CSS is collected by this singleton and output once in wp_head at priority 100 (after all blocks have rendered).
CSS is automatically minified in production (when WP_DEBUG is not true).
Source
File: lib/class-method-css-collector.php
Properties
| Property | Visibility | Type | Description |
|---|---|---|---|
$instance | private static | ?self | Singleton instance |
$styles | private | array | Collected CSS entries keyed by block ID |
$has_output | private | bool | Whether styles have already been output |
Methods
instance(): self (static)
instance(): self (static)Returns the singleton instance. Creates it on first call.
add( string $css, string $block_id = '', int $priority = 10 ): void
add( string $css, string $block_id = '', int $priority = 10 ): voidAdds CSS from a block render callback.
| Param | Type | Description |
|---|---|---|
$css | string | CSS rules without <style> tags. |
$block_id | string | Optional unique ID to prevent duplicates. |
$priority | int | Sort order — lower numbers output first. Default 10. |
Notes:
- Silently discards CSS added after
output_styles()has fired. - Duplicate
$block_idvalues are ignored.
output_styles(): void
output_styles(): voidSorts collected CSS by priority, concatenates, optionally minifies, and outputs as <style id="block-instance-styles">. Hooked to wp_head at priority 100.
minify( string $css ): string (private)
minify( string $css ): string (private)Strips comments, collapses whitespace, and removes spaces around CSS punctuation.
Global Helper
method_collect_css( string $css, string $block_id = '', int $priority = 10 ): voidConvenience wrapper: Method_CSS_Collector::instance()->add( $css, $block_id, $priority ).
Usage
// In a block render callback:
$css = method_get_block_responsive_styles( $attrs, $cssargs, ['base', 'mobile', 'tablet', 'wide'], false );
method_collect_css( $css, '#' . $methodId, 10 );Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |