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:1519
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=valueinput. 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
- Scaling
- Scales with input
- Instructions
- 20–68
- Plugin surface
- None
- Called by
- 2
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 82.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_kses_attr_check()
Further down the call graph this can also reach query, option, cache, serialize 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 · 8 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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 20–32 | strtolower(), strtolower() |
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) | 33–47 | strtolower(), strtolower(), preg_match() |
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low === "style" | 34–42 | strtolower(), strtolower(), safecss_filter_attr() |
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low !== "style" && !$allowed_attr && !wp_kses_check_attr_val() | 43 | strtolower(), strtolower(), wp_kses_check_attr_val() |
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low === "style" | 46–57 | strtolower(), strtolower(), preg_match(), safecss_filter_attr() |
isset($allowed_html[$element_low]) && isset($allowed_attr[$name_low]) && $name_low === "style" && !empty($new_value) && !$allowed_attr && !wp_kses_check_attr_val() | 53 | strtolower(), strtolower(), safecss_filter_attr(), wp_kses_check_attr_val() |
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low !== "style" && !$allowed_attr && !wp_kses_check_attr_val() | 55–58 | strtolower(), strtolower(), preg_match(), wp_kses_check_attr_val() |
isset($allowed_html[$element_low]) && $name_low && !empty($allowed_attr) && preg_match() && $name_low === "style" && !empty($new_value) && !$allowed_attr && !wp_kses_check_attr_val() | 65–68 | strtolower(), strtolower(), preg_match(), safecss_filter_attr(), wp_kses_check_attr_val() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 82 | 20–68 | 12 | |
| 8.5 | 82 | 20–68 | 12 | |
| 8.4 | 82 | 20–68 | 12 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 88 | 20–74 | 12 | |
| 8.2 | 88 | 20–74 | 12 | |
| 8.1 | 88 | 20–74 | 12 | |
| 7.4 | 88 | 20–74 | 12 |
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
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- safecss_filter_attr()Filters an inline style attribute and removes disallowed rules.
- wp_kses_check_attr_val()Performs different checks for attribute values.
Used by · 2
- wp_kses_attr()Removes all attributes, if none are allowed for this element.
- wp_kses_one_attr()Filters one HTML attribute and ensures its value is allowed.
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 ) { $new_value = safecss_filter_attr( $value ); if ( empty( $new_value ) ) { $name = ''; $value = ''; $whole = ''; return false; } $whole = str_replace( $value, $new_value, $whole ); $value = $new_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.
Signature, return type and hooks compared across 5 parsed releases.
data-* wildcard attributes.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 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.