wppaste
WordPress

wp_kses_hair( string $attr, string[] $allowed_protocols ): array<string,

Since
1.0.0, 7.0.0
Source
wp-includes/kses.php:1623
Given a string of HTML attributes and values, parse into a structured attribute list.

Description

This function performs a number of transformations while parsing attribute strings:

  • It normalizes attribute values and surrounds them with double quotes.
  • It normalizes HTML character references inside attribute values.
  • It removes “bad” URL protocols from attribute values.

Otherwise this reads the attributes as if they were part of an HTML tag. It performs these transformations to lower the risk of mis-parsing down the line and to perform URL sanitization in line with the rest of the kses subsystem. Importantly, it does not decode the attribute values, meaning that special HTML syntax characters will be left with character references in the value property.

Example:

$attrs = wp_kses_hair( 'class="is-wide" inert data-lazy=\'&lt;img&#00062\' =/🐮=/' );
$attrs === array(
 'class' => array( 'name' => 'class', 'value' => 'is-wide', 'whole' => 'class="is-wide"', 'vless' => 'n' ),
 'inert' => array( 'name' => 'inert', 'value' => '', 'whole' => 'inert', 'vless' => 'y' ),
 'data-lazy' => array( 'name' => 'data-lazy', 'value' => '&lt;img&gt;', 'whole' => 'data-lazy="&lt;img&gt;"', 'vless' => 'n' ),
 '=' => array( 'name' => '=', 'value' => '', 'whole' => '=', 'vless' => 'y' ),
 '🐮' => array( 'name' => '🐮', 'value' => '/', 'whole' => '🐮="/"', 'vless' => 'n' ),
);

Compatibility

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

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attrstring
Attribute list from HTML element to closing HTML element tag.
$allowed_protocolsstring[]
Array of allowed URL protocols.

Return value

array<string,
array{name: string, value: string, whole: string, vless: 'y'|'n'}> Array of attribute information after parsing.

Performance profile

How much work a call to wp_kses_hair() 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
22–28

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below wp_kses_hair()

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always22–28wp_kses_uri_attributes(), ->next_token(), ->get_attribute_names_with_prefix()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6822–289
8.56822–289
8.46822–2897 fewer instructions than PHP 8.3
8.37522–289
8.27522–289
8.17522–2891 fewer instruction than PHP 7.4
7.47622–289

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.

Uses · 3

Used by · 4

Source code

function wp_kses_hair( $attr, $allowed_protocols ) {	$attributes = array();	$uris       = wp_kses_uri_attributes(); 	$processor = new WP_HTML_Tag_Processor( "<wp {$attr}>" );	$processor->next_token(); 	$attribute_names = $processor->get_attribute_names_with_prefix( '' );	if ( null === $attribute_names || 0 === count( $attribute_names ) ) {		return $attributes;	} 	$syntax_characters = array(		'&' => '&amp;',		'<' => '&lt;',		'>' => '&gt;',		"'" => '&apos;',		'"' => '&quot;',	); 	foreach ( $attribute_names as $name ) {		$value   = $processor->get_attribute( $name );		$is_bool = true === $value;		if ( is_string( $value ) && in_array( $name, $uris, true ) ) {			$value = wp_kses_bad_protocol( $value, $allowed_protocols );		} 		// Reconstruct and normalize the attribute value.		$recoded = $is_bool ? '' : strtr( $value, $syntax_characters );		$whole   = $is_bool ? $name : "{$name}=\"{$recoded}\""; 		$attributes[ $name ] = array(			'name'  => $name,			'value' => $recoded,			'whole' => $whole,			'vless' => $is_bool ? 'y' : 'n',		);	} 	return $attributes;}

Changelog

Introduced in 1.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

7.0.4
Return type changed from array[] to array<string,.verified against source
7.0.0
Reliably parses HTML via the HTML API.from the docblock
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/kses.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.