wp_kses_attr_parse

The timeline below displays how wordpress function wp_kses_attr_parse has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.7

/**
 * Finds all attributes of an HTML element.
 *
 * Does not modify input.  May return "evil" output.
 *
 * Based on `wp_kses_split2()` and `wp_kses_attr()`.
 *
 * @since 4.2.3
 *
 * @param string $element HTML element.
 * @return array|false List of attributes found in the element. Returns false on failure.
 */
function wp_kses_attr_parse($element)
{
    $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
    if (1 !== $valid) {
        return false;
    }
    $begin = $matches[1];
    $slash = $matches[2];
    $elname = $matches[3];
    $attr = $matches[4];
    $end = $matches[5];
    if ('' !== $slash) {
        // Closing elements do not get parsed.
        return false;
    }
    // Is there a closing XHTML slash at the end of the attributes?
    if (1 === preg_match('%\s*/\s*$%', $attr, $matches)) {
        $xhtml_slash = $matches[0];
        $attr = substr($attr, 0, -strlen($xhtml_slash));
    } else {
        $xhtml_slash = '';
    }
    // Split it.
    $attrarr = wp_kses_hair_parse($attr);
    if (false === $attrarr) {
        return false;
    }
    // Make sure all input is returned by adding front and back matter.
    array_unshift($attrarr, $begin . $slash . $elname);
    array_push($attrarr, $xhtml_slash . $end);
    return $attrarr;
}

WordPress Version: 5.4

/**
 * Finds all attributes of an HTML element.
 *
 * Does not modify input.  May return "evil" output.
 *
 * Based on `wp_kses_split2()` and `wp_kses_attr()`.
 *
 * @since 4.2.3
 *
 * @param string $element HTML element.
 * @return array|bool List of attributes found in the element. Returns false on failure.
 */
function wp_kses_attr_parse($element)
{
    $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
    if (1 !== $valid) {
        return false;
    }
    $begin = $matches[1];
    $slash = $matches[2];
    $elname = $matches[3];
    $attr = $matches[4];
    $end = $matches[5];
    if ('' !== $slash) {
        // Closing elements do not get parsed.
        return false;
    }
    // Is there a closing XHTML slash at the end of the attributes?
    if (1 === preg_match('%\s*/\s*$%', $attr, $matches)) {
        $xhtml_slash = $matches[0];
        $attr = substr($attr, 0, -strlen($xhtml_slash));
    } else {
        $xhtml_slash = '';
    }
    // Split it.
    $attrarr = wp_kses_hair_parse($attr);
    if (false === $attrarr) {
        return false;
    }
    // Make sure all input is returned by adding front and back matter.
    array_unshift($attrarr, $begin . $slash . $elname);
    array_push($attrarr, $xhtml_slash . $end);
    return $attrarr;
}

WordPress Version: 5.1

/**
 * Finds all attributes of an HTML element.
 *
 * Does not modify input.  May return "evil" output.
 *
 * Based on `wp_kses_split2()` and `wp_kses_attr()`.
 *
 * @since 4.2.3
 *
 * @param string $element HTML element.
 * @return array|bool List of attributes found in the element. Returns false on failure.
 */
function wp_kses_attr_parse($element)
{
    $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
    if (1 !== $valid) {
        return false;
    }
    $begin = $matches[1];
    $slash = $matches[2];
    $elname = $matches[3];
    $attr = $matches[4];
    $end = $matches[5];
    if ('' !== $slash) {
        // Closing elements do not get parsed.
        return false;
    }
    // Is there a closing XHTML slash at the end of the attributes?
    if (1 === preg_match('%\s*/\s*$%', $attr, $matches)) {
        $xhtml_slash = $matches[0];
        $attr = substr($attr, 0, -strlen($xhtml_slash));
    } else {
        $xhtml_slash = '';
    }
    // Split it
    $attrarr = wp_kses_hair_parse($attr);
    if (false === $attrarr) {
        return false;
    }
    // Make sure all input is returned by adding front and back matter.
    array_unshift($attrarr, $begin . $slash . $elname);
    array_push($attrarr, $xhtml_slash . $end);
    return $attrarr;
}

WordPress Version: .10

/**
 * Finds all attributes of an HTML element.
 *
 * Does not modify input.  May return "evil" output.
 *
 * Based on wp_kses_split2() and wp_kses_attr()
 *
 * @since 4.2.3
 *
 * @param string $element HTML element/tag
 * @return array|bool List of attributes found in $element. Returns false on failure.
 */
function wp_kses_attr_parse($element)
{
    $valid = preg_match('%^(<\s*)(/\s*)?([a-zA-Z0-9]+\s*)([^>]*)(>?)$%', $element, $matches);
    if (1 !== $valid) {
        return false;
    }
    $begin = $matches[1];
    $slash = $matches[2];
    $elname = $matches[3];
    $attr = $matches[4];
    $end = $matches[5];
    if ('' !== $slash) {
        // Closing elements do not get parsed.
        return false;
    }
    // Is there a closing XHTML slash at the end of the attributes?
    if (1 === preg_match('%\s*/\s*$%', $attr, $matches)) {
        $xhtml_slash = $matches[0];
        $attr = substr($attr, 0, -strlen($xhtml_slash));
    } else {
        $xhtml_slash = '';
    }
    // Split it
    $attrarr = wp_kses_hair_parse($attr);
    if (false === $attrarr) {
        return false;
    }
    // Make sure all input is returned by adding front and back matter.
    array_unshift($attrarr, $begin . $slash . $elname);
    array_push($attrarr, $xhtml_slash . $end);
    return $attrarr;
}