[wp-trac] [WordPress Trac] #65120: Add `wp_elements_block_supports_class_name` filter to `wp_get_elements_class_name()`

WordPress Trac noreply at wordpress.org
Thu Apr 23 18:57:36 UTC 2026


#65120: Add `wp_elements_block_supports_class_name` filter to
`wp_get_elements_class_name()`
-------------------------+-------------------------------------------------
 Reporter:               |      Owner:  (none)
  baikare.sandeep007     |
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Editor       |    Version:  trunk
 Severity:  normal       |   Keywords:  has-patch needs-testing has-test-
  Focuses:  template     |  info
-------------------------+-------------------------------------------------
 == 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`

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/65120>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list