Allow special HTML tags in comments

I recently faced a problem - I needed any visitor to the site to be able to use the html tag <pre class=""> in the comments.

But, as you probably know, WordPress allows adding tags in comments in a limited way, more precisely, only such html tags are allowed in comments:

<a href="" title=""> <abbr title="">
<acronym title=""> <b> <blockquote cite="">
<cite> <code> <del datetime="">
<em> <i> <q cite=""> <strike> <strong>
How to expand this list of allowed html tags in comments?

I tried to answer this question once, and not without help from the outside. And all I could do was surgically fix the WordPress file: wp-includes/kses.php, which actually specifies the allowed tags in comments. However, it is not a good idea to manually edit the engine files, because all the changes will be erased during the update and you will have to change everything again.

An alternative solution appeared to me in the form of a hook, but I couldn't find such a hook. In the end, understanding how it all works, a bright and extremely simple thought came to me: to call the variable in which the allowed tags are specified globally and attach my additional tags to it smile

It looks like this:

// add allowed tags in comments for visitors
global $allowedtags;

$allowedtags_add = array (
	'pre' => array(
		'class' => true,
		'code'  => true
	'img' => array(
		'alt'    => true,
		'height' => true,
		'src'    => true,
		'width'  => true,
	'ul' => true,
	'li' => true,
	'ol' => true,

$allowedtags = array_merge( $allowedtags, $allowedtags_add );

Genius is simple! smile

The code above allows writing the <pre> tag with the attributes class and code and the ul li ol tags in comments. Similarly, you can add any other tags. However, it is not advisable to abuse this for security reasons.

To make everything work, this code needs to be placed in the functions.php file of your template. If you don't have such a file in your template, you need to create it.

Allow tags/tag attributes in post content

To expand the list of allowed tags for post content, do the same thing, but with the global variable $allowedposttags.