W3cubDocs

/WordPress

WP_HTML_Processor::get_attribute( string $name ): string|true|null

Returns the value of a requested attribute from a matched tag opener if that attribute exists.

Description

Example:

$p = WP_HTML_Processor::create_fragment( '<div enabled class="test" data-test-id="14">Test</div>' );
$p->next_token() === true;
$p->get_attribute( 'data-test-id' ) === '14';
$p->get_attribute( 'enabled' ) === true;
$p->get_attribute( 'aria-label' ) === null;

$p->next_tag() === false;
$p->get_attribute( 'class' ) === null;

Parameters

$namestringrequired
Name of attribute whose value is requested.

Return

string|true|null Value of attribute or null if not available. Boolean attributes return true.

Source

/*
 * > A start tag whose tag name is "noscript", if the scripting flag is disabled
 */

Changelog

Version Description
6.6.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_html_processor/get_attribute