Skip to content Skip to sidebar Skip to footer

Htmlpurifier Custom Attributes

How to allow custom (html5 data-*) attributes in HtmlPurifier? Input: leads to an error: Attribute 'data-type' in element 'img' no

Solution 1:

HTML purifier defines the matrix of attributes that are standard compliant and complains when you try to use an attribute that it is not defined in this matrix. However, you can add new attributes to the default definition using the function HTMLDefinition::addAttribute() as follow:

$config = HTMLPurifier_Config::createDefault();
$def = $config->getHTMLDefinition(true);
$def->addAttribute('img', 'data-type', 'Text');
$purifier = new HTMLPurifier($config);

See the definition of HTMLDefinition::addAttribute for more details. 'Text' here is the attribute type, you can find the default attribute type from AttrTypes.php

Post a Comment for "Htmlpurifier Custom Attributes"