wp_kses_hair_parse() WordPress Function

The wp_kses_hair_parse() function in WordPress allows you to specify which HTML elements and attributes are allowed in your content. This is useful for preventing malicious code from being injected into your posts and pages.

wp_kses_hair_parse( string $attr ) #

Builds an attribute list from string containing attributes.


Description

Does not modify input. May return "evil" output. In case of unexpected input, returns false instead of stripping things.

Based on wp_kses_hair() but does not return a multi-dimensional array.


Top ↑

Parameters

$attr

(string)(Required)Attribute list from HTML element to closing HTML element tag.


Top ↑

Return

(array|false) List of attributes found in $attr. Returns false on failure.


Top ↑

Source

File: wp-includes/kses.php

function wp_kses_hair_parse( $attr ) {
	if ( '' === $attr ) {
		return array();
	}

	// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- don't remove regex indentation
	$regex =
		'(?:'
		.     '[_a-zA-Z][-_a-zA-Z0-9:.]*' // Attribute name.
		. '|'
		.     '\[\[?[^\[\]]+\]\]?'        // Shortcode in the name position implies unfiltered_html.
		. ')'
		. '(?:'               // Attribute value.
		.     '\s*=\s*'       // All values begin with '='.
		.     '(?:'
		.         '"[^"]*"'   // Double-quoted.
		.     '|'
		.         "'[^']*'"   // Single-quoted.
		.     '|'
		.         '[^\s"\']+' // Non-quoted.
		.         '(?:\s|$)'  // Must have a space.
		.     ')'
		. '|'
		.     '(?:\s|$)'      // If attribute has no value, space is required.
		. ')'
		. '\s*';              // Trailing space is optional except as mentioned above.
	// phpcs:enable

	// Although it is possible to reduce this procedure to a single regexp,
	// we must run that regexp twice to get exactly the expected result.

	$validation = "%^($regex)+$%";
	$extraction = "%$regex%";

	if ( 1 === preg_match( $validation, $attr ) ) {
		preg_match_all( $extraction, $attr, $attrarr );
		return $attrarr[0];
	} else {
		return false;
	}
}


Top ↑

Changelog

Changelog
VersionDescription
4.2.3Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.