By default, the WordPress comment form allows users to include basic HTML tags in their comments, such as <p>, <br>, <strong>, and others. In most cases, these basic tags are sufficient for standard formatting. However, for certain websites, you might need to allow additional tags to support specific features.
For example, on a server configuration sharing site, users may need to share Apache .htaccess code via comments. Because WordPress restricts allowed HTML tags, when a user tries to post a comment containing Apache directives like <IfModule>, <Directory>, or <VirtualHost>, the WordPress comment filtering system will automatically strip these tags. This applies to any non-permitted markup, whether it’s valid HTML or other custom tags.
To remove this restriction, WordPress provides a straightforward way to customize the list of allowed comment tags. In the scenario described above, you can add the following function to permit users to include the necessary tags in their comments:
function wprs_add_allowed_tags() {
global $allowedtags;
// Add support for standard layout tags with attributes
$allowedtags['p'] = array('class' => true, 'id' => true);
$allowedtags['ul'] = array('class' => true, 'id' => true);
$allowedtags['ol'] = array('class' => true, 'id' => true);
$allowedtags['li'] = array('class' => true, 'id' => true);
$allowedtags['pre'] = array('class' => true, 'id' => true);
// Add support for links and spans with custom data attributes
$allowedtags['a'] = array('class' => true, 'id' => true, 'title' => true, 'data-url' => true, 'data-date' => true, 'data-title' => true, 'href' => true);
$allowedtags['span'] = array('class' => true, 'id' => true, 'title' => true, 'data-url' => true, 'data-date' => true, 'data-title' => true);
$allowedtags['strong'] = array('class' => true, 'id' => true, 'title' => true, 'data-url' => true, 'data-date' => true, 'data-title' => true);
// Add support for Apache directives
$allowedtags['ifmodule'] = array('class' => true, 'mod_rewrite.c' => true, 'mod_alias.c' => true, 'mod_auth.c' => true);
$allowedtags['directory'] = array();
$allowedtags['virtualhost'] = array();
// Add custom internal tags
$allowedtags['info'] = array();
$allowedtags['note'] = array();
$allowedtags['update'] = array();
}
add_action('init', 'wprs_add_allowed_tags', 11);
In addition to the baseline HTML tags allowed by default, this snippet adds the Apache directives mentioned earlier, as well as some internal tags used for record-keeping.
If you require similar functionality, you can add the code above directly to your theme’s functions.php file or a custom plugin, and then adjust the list of allowed tags to match your specific requirements.
