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

protected by Community Jan 18 '18 at 13:24

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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