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 & Milk">`.
$processor->set_attribute( 'title', 'Eggs & Milk' );
// Renders “Eggs & Milk” in a browser, encoded as `<abbr title="Eggs &amp; Milk">`.
$processor->set_attribute( 'title', 'Eggs & 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
trueis passed as the value, then only the attribute name is added to the tag. - When
falseis passed, the attribute gets removed if it existed before.
Parameters
$namestringrequired- The attribute name to target.
$valuestring|boolrequired- The new attribute value.
Source
public function set_attribute( $name, $value ): bool {
return $this->is_virtual() ? false : parent::set_attribute( $name, $value );
}
User Contributed Notes
You must log in before being able to contribute a note or feedback.