wppaste
WordPress

wp_kses_attr_check( string $name, string $value, string $whole, string $vless, string $element, array $allowed_html ): bool

Since
4.2.3, 5.0.0
Source
wp-includes/kses.php:1605
Determines whether an attribute is allowed.

Compatibility

WordPress
since 5.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

$namestring
The attribute name. Passed by reference. Returns empty string when not allowed.
$valuestring
The attribute value. Passed by reference. Returns a filtered value.
$wholestring
The name=value input. Passed by reference. Returns filtered input.
$vlessstring
Whether the attribute is valueless. Use 'y' or 'n'.
$elementstring
The name of the element to which this attribute belongs.
$allowed_htmlarray
The full list of allowed elements and attributes.

Return value

bool
Whether or not the attribute is allowed.

Performance profile

How much work a call to wp_kses_attr_check() 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
20–78

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 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_attr_check()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

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

WhenInstructionsCalls it makes
always20–32strtolower(), strtolower()
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr)33–47strtolower(), strtolower(), preg_match()
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low === "style"38–44strtolower(), strtolower(), ::WP_HTML_Decoder(), safecss_filter_attr()
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low !== "style" && !$allowed_attr && !wp_kses_check_attr_val()43strtolower(), strtolower(), wp_kses_check_attr_val()
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low === "style" && !empty($new_value) && $new_value !== false48–52strtolower(), strtolower(), ::WP_HTML_Decoder(), safecss_filter_attr(), esc_attr()
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low === "style"50–59strtolower(), strtolower(), preg_match(), ::WP_HTML_Decoder(), safecss_filter_attr()
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low !== "style" && !$allowed_attr && !wp_kses_check_attr_val()55–58strtolower(), strtolower(), preg_match(), wp_kses_check_attr_val()
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low === "style" && !empty($new_value) && $new_value === false && !$allowed_attr && !wp_kses_check_attr_val()55strtolower(), strtolower(), ::WP_HTML_Decoder(), safecss_filter_attr(), wp_kses_check_attr_val()
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low === "style" && !empty($new_value) && $new_value !== false60–67strtolower(), strtolower(), preg_match(), ::WP_HTML_Decoder(), safecss_filter_attr(), esc_attr()
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low === "style" && !empty($new_value) && $new_value !== false && !$allowed_attr && !wp_kses_check_attr_val()63strtolower(), strtolower(), ::WP_HTML_Decoder(), safecss_filter_attr(), esc_attr(), wp_kses_check_attr_val()
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low === "style" && !empty($new_value) && $new_value === false && !$allowed_attr && !wp_kses_check_attr_val()67–70strtolower(), strtolower(), preg_match(), ::WP_HTML_Decoder(), safecss_filter_attr(), wp_kses_check_attr_val()
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low === "style" && !empty($new_value) && $new_value !== false && !$allowed_attr && !wp_kses_check_attr_val()75–78strtolower(), strtolower(), preg_match(), ::WP_HTML_Decoder(), safecss_filter_attr(), esc_attr(), wp_kses_check_attr_val()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9220–7813
8.59220–7813
8.49220–78136 fewer instructions than PHP 8.3
8.39820–8413
8.29820–8413
8.19820–8413
7.49820–8413

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 · 5

Used by · 2

Source code

function wp_kses_attr_check( &$name, &$value, &$whole, $vless, $element, $allowed_html ) {	$name_low    = strtolower( $name );	$element_low = strtolower( $element ); 	if ( ! isset( $allowed_html[ $element_low ] ) ) {		$name  = '';		$value = '';		$whole = '';		return false;	} 	$allowed_attr = $allowed_html[ $element_low ]; 	if ( ! isset( $allowed_attr[ $name_low ] ) || '' === $allowed_attr[ $name_low ] ) {		/*		 * Allow `data-*` attributes.		 *		 * When specifying `$allowed_html`, the attribute name should be set as		 * `data-*` (not to be mixed with the HTML 4.0 `data` attribute, see		 * https://www.w3.org/TR/html40/struct/objects.html#adef-data).		 *		 * Note: the attribute name should only contain `A-Za-z0-9_-` chars.		 */		if ( str_starts_with( $name_low, 'data-' ) && ! empty( $allowed_attr['data-*'] )			&& preg_match( '/^data-[a-z0-9_-]+$/', $name_low, $match )		) {			/*			 * Add the whole attribute name to the allowed attributes and set any restrictions			 * for the `data-*` attribute values for the current element.			 */			$allowed_attr[ $match[0] ] = $allowed_attr['data-*'];		} else {			$name  = '';			$value = '';			$whole = '';			return false;		}	} 	if ( 'style' === $name_low ) {		$decoded_value = WP_HTML_Decoder::decode_attribute( $value );		$new_value     = safecss_filter_attr( $decoded_value ); 		if ( empty( $new_value ) ) {			$name  = '';			$value = '';			$whole = '';			return false;		} 		if ( $new_value !== $decoded_value ) {			$encoded_value = esc_attr( $new_value );			$whole         = str_replace( $value, $encoded_value, $whole );			$value         = $encoded_value;		}	} 	if ( is_array( $allowed_attr[ $name_low ] ) ) {		// There are some checks.		foreach ( $allowed_attr[ $name_low ] as $currkey => $currval ) {			if ( ! wp_kses_check_attr_val( $value, $vless, $currkey, $currval ) ) {				$name  = '';				$value = '';				$whole = '';				return false;			}		}	} 	return true;}

Changelog

Introduced in 4.2.3. Unchanged from 6.7.7 through 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.

5.0.0
Added support for data-* wildcard attributes.from the docblock
4.2.3
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.