Make WordPress Core

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: baikaresandeep007's profile baikare.sandeep007 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:

  1. Performance optimization – Replace serialize() + md5() with more efficient class name generation for complex blocks.
  2. Cache-friendly class names – Generate human-readable names for easier debugging.
  3. Custom hashing logic – Implement alternative algorithms or naming conventions.
  4. 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_name and $block parameters.
  • Filter return value properly replaces the default class name.

Additional Notes

  • @since tag used: 7.1.0
  • Props: @baikare.sandeep007

Attachments (1)

wp-elements-block-support-hook.patch (785 bytes) - added by baikare.sandeep007 3 weeks ago.

Download all attachments as: .zip

Change History (7)

#1 @baikare.sandeep007
3 weeks ago

  • Focuses performance coding-standards added

#2 @baikare.sandeep007
3 weeks ago

  • Keywords needs-docs added

#3 @baikare.sandeep007
3 weeks ago

  • Keywords needs-docs removed

This ticket was mentioned in Slack in #core by baikaresandeep007-1. View the logs.


3 weeks ago

#5 @westonruter
2 weeks ago

  • Keywords 2nd-opinion added

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.

#6 @baikare.sandeep007
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.

Note: See TracTickets for help on using tickets.