Customize the HTML tags allowed in WordPress comments

By default, WordPress comment forms allow users to include basicHTMLtags, which include<p>、 <br>、 <strong > etc. In most cases, these basic tags are sufficient to add basic formatting. However, for some sites, more tags may be needed to achieve the desired functionality.

For example, on a server-configured sharing site, users need to share Apache .htaccess code via comments. However, due to the limited HTML tags allowed by WordPress, when a user tries to share a .htaccess code that contains Apache tags like, <Directory > maybe<VirtualHost> criticismwhenWordPress comment filtering system onRemove these tags. Any disallowed markup (whether HTML markup or other markup) this will enjoy.

To remove this limitation, WordPress provides an easy way to customize the allowed comment tags, in the above case we can add the following function to allow the user to add the desired tags to the comment:

function wprs_add_allowed_tags() {

global $allowedtags.

$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);

$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);

$allowedtags['ifmodule'] = array('class' => true, 'mod_rewrite.c' => true, 'mod_alias.c' => true, 'mod_auth.c' => true);
$allowedtags['directory'] = array();
$allowedtags['virtualhost'] = array();

$allowedtags['info'] = array();
$allowedtags['note'] = array();
$allowedtags['update'] = array();

}
add_action('init', 'wprs_add_allowed_tags', 11);

In addition to the basic HTML tags that are allowed by default, this code adds the Apache tags mentioned earlier, as well as some internal tags that are used for record-keeping and such.

If you need similar functionality, you can just add the above code directly to the theme or plugin, and then just adjust the allowed tags in it according to your needs.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *