wp_html_custom_data_attribute_name( string $js_dataset_name ): string|null
- Since
- 6.9.0
- Source
wp-includes/script-loader.php:4153
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
datasetproperty 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
- Scaling
- Scales with input
- Instructions
- 5–32
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 50.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
$end === 0 | 5 | none |
$end !== 0 | 11–19 | strcspn() |
$end !== 0 && $end === false && !$at && $was_at | 24 | strcspn(), strtolower() |
$end !== 0 && $end === false && $at && $end && !$was_at | 27 | strcspn(), strcspn() |
$end !== 0 && $end === false && $at && $end && $was_at | 32 | strcspn(), strcspn(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 50 | 5–32 | 5 | |
| 8.5 | 50 | 5–32 | 5 | |
| 8.4 | 50 | 5–32 | 5 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 56 | 5–35 | 5 | |
| 8.2 | 56 | 5–35 | 5 | |
| 8.1 | 56 | 5–35 | 5 | |
| 7.4 | 56 | 5–35 | 5 |
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.
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.