Opened 3 weeks ago
Last modified 2 weeks ago
#65120 new enhancement
Add `wp_elements_block_supports_class_name` filter to `wp_get_elements_class_name()`
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | trunk |
| Component: | Editor | Keywords: | has-patch needs-testing has-test-info 2nd-opinion |
| Focuses: | template, performance, coding-standards | Cc: |
Description
Description
This patch introduces a new filter, wp_elements_block_supports_class_name, to the wp_get_elements_class_name() function. This allows developers to customize the generated CSS class name for block elements.
Current Behavior
Currently, the function generates a class name by serializing the entire block array and creating an MD5 hash:
$class_name = 'wp-elements-' . md5( serialize( $block ) );
There is no mechanism for developers to modify or override the generated class name without hijacking the entire function.
Proposed Change
The patch adds a filter to enable customization:
return apply_filters( 'wp_elements_block_supports_class_name', $class_name, $block );
Use Cases
This filter is valuable for:
- Performance optimization – Replace
serialize()+md5()with more efficient class name generation for complex blocks. - Cache-friendly class names – Generate human-readable names for easier debugging.
- Custom hashing logic – Implement alternative algorithms or naming conventions.
- Integration with build tools – Align class names with external CSS processes.
Example
A developer could use the filter to create more readable class names:
add_filter( 'wp_elements_block_supports_class_name', function( $class_name, $block ) {
if ( isset( $block['attrs']['id'] ) ) {
return 'wp-elements-' . $block['blockName'] . '-' . $block['attrs']['id'];
}
return $class_name;
}, 10, 2 );
Impact
- Backward compatibility: Yes – existing behavior remains unchanged when the filter is not used.
- Performance overhead: Negligible – only executes when hooked.
- Functional change: None – additive only.
Testing
Patch verified:
- Existing functionality unchanged without filter.
- Filter properly receives both
$class_nameand$blockparameters. - Filter return value properly replaces the default class name.
Additional Notes
@sincetag used:7.1.0- Props:
@baikare.sandeep007
Attachments (1)
Change History (7)
This ticket was mentioned in Slack in #core by baikaresandeep007-1. View the logs.
3 weeks ago
#6
@
2 weeks ago
I thought the class names starting with wp-elements-* would be defined in a CSS file, but it turns out they’re generated dynamically within the same elements.php file. Because of that, I don’t think it will break the UI. However, it would definitely give developers more flexibility to use user-friendly class names.
I'm not sure this is a good idea. I don't see a clear need for it. Note where the return value of the funtion is used:
https://github.com/WordPress/wordpress-develop/blob/acebfd0ac218c455d25ae9e06879bc42da18b9c9/src/wp-includes/block-supports/elements.php#L160-L220
By allowing the class name to be filtered, I worry this would introduce breakages.