0

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');
2

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);
  • The issue was not related to the code in this question, but with my TinyMCE configuration. Still changed comment_post to pre_comment_content just because it seems like a better hook. Don't forget to pass and return the $comment_content variable in your function if you use this! – Swen Sep 20 '17 at 15:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.