Tips on Expanding the allowed HTML tags in comments
WordPress allows a select few HTML tags within the content of post comments. This is an awesome feature, of course, because it prevents XSS security holes and other malicious code from being injected by spammers, hackers.
Unfortunately there are many other tags that bloggers may want to support; for example, tech bloggers may want to support PRE
tags so commenters can post code. Luckily it’s quite easy to allow more tags within your WordPress comments.
In WordPress tags that are allowed in comments are stored in the $allowedtags global variable
. You could try adding HTML elements to that list (the key is the tag name, the value is an array of allowed attributes).
Tutorial on how to modify complete comment form?
To Add
more elements:
Add below code to themes’ functions.php
file.
// Create function which allows more tags within comments function crunchify_allow_html_attributes_in_commentform() { global $allowedtags; $allowedtags['pre'] = array('class'=>array()); $allowedtags['p'] = array('class'=>array()); $allowedtags['code'] = array('class'=>array()); $allowedtags['strong'] = array('class'=>array()); $allowedtags['href'] = array('class'=>array()); } // Add WordPress hook to use the function add_action('init', 'crunchify_allow_html_attributes_in_commentform',11);
The global $allowedtags
variable holds an array of allowed comment tags, so adding the pre
key will allow PRE
elements within comments. The class
key within the pre
array allows the class
attribute for any PRE tags posted within the comment, so not only can you allow additional HTML tags, but you can also specify allowed attributes too!
To Remove
more elements:
function crunchify_remove_html_attributes_in_commentform() { global $allowedtags; // remove crunchify_tags_to_remove tags $crunchify_tags_to_remove = array( 'blockquote', 'cite', 'code', 'del', 'pre' ); foreach ( $crunchify_tags_to_remove as $tag ) unset( $allowedtags[$tag] ); // add wanted tags $crunchify_newTags = array( 'span' => array( 'lang' => array()), 'u' => array() ); $allowedtags = array_merge( $allowedtags, $crunchify_newTags ); } add_action('init', 'crunchify_remove_html_attributes_in_commentform', 11 );
Above functions add/remove the comment_post
hook so it only adds desired tags when a comment is posted.