WP_HTML_Processor::set_attribute( string $name, string|bool $value ): bool

Updates or creates a new attribute on the currently matched tag with the passed value.

Description

This function handles all necessary HTML encoding. Provide normal, unescaped string values.
The HTML API will encode the strings appropriately so that the browser will interpret them as the intended value.

Example:

// Renders “Eggs & Milk” in a browser, encoded as `<abbr title="Eggs &amp; Milk">`.
$processor->set_attribute( 'title', 'Eggs & Milk' );

// Renders “Eggs &amp; Milk” in a browser, encoded as `<abbr title="Eggs &amp;amp; Milk">`.
$processor->set_attribute( 'title', 'Eggs &amp; Milk' );

// Renders `true` as `<abbr title>`.
$processor->set_attribute( 'title', true );

// Renders without the attribute for `false` as `<abbr>`.
$processor->set_attribute( 'title', false );

Special handling is provided for boolean attribute values:

  • When true is passed as the value, then only the attribute name is added to the tag.
  • When false is passed, the attribute gets removed if it existed before.

Parameters

$namestringrequired
The attribute name to target.
$valuestring|boolrequired
The new attribute value.

Return

bool Whether an attribute value was set.

Source

public function set_attribute( $name, $value ): bool {
	return $this->is_virtual() ? false : parent::set_attribute( $name, $value );
}

Changelog

VersionDescription
6.9.0Escapes all character references instead of trying to avoid double-escaping.
6.6.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.