Miss All Sunday


Dime si me estoy volviendo loco, dime por favor si a ti te pasa igual

HTML Purifier


6bvqglH.png

HTML Purifier is a filter library that allows you to sanitize user input. It has a definition list of allowed HTML elements which is fine for most cases, however, if you want to implement structured data to automatically add the needed tags to an image for example, you will need to add some new elements and attributes to its list otherwise they will be removed.


Here is a little tip on how to add the needed exceptions to HTML Purifier to allow meta tags:

	$config = \HTMLPurifier_Config::createDefault();
	$def = $config->getHTMLDefinition(true);
	$meta = $def->addElement(
		'meta',
		'Inline',
		'Empty',
		'Common',
		[
			'itemprop' => 'Text',
			'content' => 'URI',
			'itemscope' => 'Bool',
			'itemtype' => 'URI',
		]
	);


Which basically covers all cases where meta tags are used. Some other elements also accept special attributes, for those cases new attributes are needed, heres an example on how to add new attributes to a div element:
	$config = \HTMLPurifier_Config::createDefault();

	$def = $config->getHTMLDefinition(true);
	$def->addAttribute('div', 'itemprop', 'Text');
	$def->addAttribute('div', 'itemscope', 'Bool');
	$def->addAttribute('div', 'itemtype', 'URI');
	$def->addAttribute('div', 'content', 'URI');


In this case we add the needed item* attributes, each one with its corresponding type. The same can be applied to other tags as well, structured data microdata are usually added to divs but can be added to any other common element such as span or img tags.
Last edited by Suki reason: typos on 6 Mar 2017, 9:40 am