wppaste
WordPress

wp_html_custom_data_attribute_name( string $js_dataset_name ): string|null

Since
6.9.0
Source
wp-includes/script-loader.php:4153
Returns a corresponding HTML attribute name for the given name, if that name were found in a JS element’s dataset property.

Description

Example:

'data-post-id' === wp_html_custom_data_attribute_name( 'postId' );
'data--before' === wp_html_custom_data_attribute_name( 'Before' );
'data---one---two---' === wp_html_custom_data_attribute_name( '-One--Two---' );

// Not every attribute name will be interpreted as a custom data attribute.
null === wp_html_custom_data_attribute_name( '/not-an-attribute/' );
null === wp_html_custom_data_attribute_name( 'no spaces' );

// Some very surprising names will; for example, a property whose name is the empty string.
'data-' === wp_html_custom_data_attribute_name( '' );

Compatibility

WordPress
since 6.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$js_dataset_namestring
Name of JS dataset property to transform.

Return value

string|null
Corresponding name of an HTML custom data attribute for the given dataset name, if possible to represent in HTML, otherwise null.

Performance profile

How much work a call to wp_html_custom_data_attribute_name() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–32

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 50.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_html_custom_data_attribute_name() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$end === 05none
$end !== 011–19strcspn()
$end !== 0 && $end === false && !$at && $was_at24strcspn(), strtolower()
$end !== 0 && $end === false && $at && $end && !$was_at27strcspn(), strcspn()
$end !== 0 && $end === false && $at && $end && $was_at32strcspn(), strcspn(), strtolower()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev505–325
8.5505–325
8.4505–3256 fewer instructions than PHP 8.3
8.3565–355
8.2565–355
8.1565–355
7.4565–355

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Source code

function wp_html_custom_data_attribute_name( string $js_dataset_name ): ?string {	$end = strlen( $js_dataset_name );	if ( 0 === $end ) {		return 'data-';	} 	/*	 * If it contains characters which would end the attribute name parsing then	 * something it’s not possible to represent this in HTML.	 */	if ( strcspn( $js_dataset_name, "=/> \t\f\r\n" ) !== $end ) {		return null;	} 	$html_name = 'data-';	$at        = 0;	$was_at    = $at; 	while ( $at < $end ) {		$next_upper_after = strcspn( $js_dataset_name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', $at );		$next_upper_at    = $at + $next_upper_after;		if ( $next_upper_at >= $end ) {			break;		} 		$prefix     = substr( $js_dataset_name, $was_at, $next_upper_at - $was_at );		$html_name .= strtolower( $prefix );		$html_name .= '-' . strtolower( $js_dataset_name[ $next_upper_at ] );		$at         = $next_upper_at + 1;		$was_at     = $at;	} 	if ( $was_at < $end ) {		$html_name .= strtolower( substr( $js_dataset_name, $was_at ) );	} 	return $html_name;}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

Signature, return type and hooks compared across 3 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/script-loader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.