Change allowed HTML tags for comments

I've set up a HTML editor for WordPress comments, and I want to change the allowed HTML tags for comments accordingly.

Some of the HTML tags also have inline styling, or classes added. I only want to allow the styling and classes that I'm expecting, but I cannot get it to work.

I want to allow only these custom classes and styles.

span class="spoiler"This is spoilered text/span

span style="text-decoration: line-through;"This text has strikethrough/span

Non-working code:

function custom_allowed_tags_comment() {

    global $allowedtags;

    $allowedtags = array(       
        'p' = array(),

        'b' = array(),
        'strong' = array(),
        'em' = array(),

        'blockquote' = array(),

        'ul' = array(),
        'li' = array(),
        'ol' = array(),

        'span' = array(
            'class' = array(
                'spoiler'
            ),
            'style' = array(
                'text-decoration: line-through;'
            )
        )
    );

} add_filter('comment_post', 'custom_allowed_tags_comment');

Topic allowedtags wp-kses comments Wordpress

Category Web


You are changing the $allowedtags too late.

On the last line, add_filter should be called for pre_comment_content instead of comment_post, and also, use different priority, something like this:

add_filter('pre_comment_content', 'custom_allowed_tags_comment', 9);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.