2

How can I remove some of the allowed HTML tags in comments/posts? For whatever reason, the following code, placed in my theme's functions.php, didn't work:

add_action('init', 'my_html_tags_code', 10);
function my_html_tags_code() {
    define('CUSTOM_TAGS', true);
    global $allowedposttags, $allowedtags;
    $allowedposttags = array(
        'strong' => array(),
        'em' => array(),
        'pre' => array(),
        'code' => array(),
        'a' => array(
          'href' => array (),
          'title' => array ())
    );

    $allowedtags = array(
        'strong' => array(),
        'em' => array(),
        'a' => array(
          'href' => array (),
          'title' => array ())
    );
}

Tags such as <div> or <pre> are still available when commenting.

2
  • <pre> isn't allowed by default. I think this setting is getting changed elsewhere. Maybe by a plugin.
    – sakibmoon
    Aug 17, 2014 at 9:12
  • I thought of that, but even after disabling all plugins, the problem remains (and the theme is developed by me, so I know it has nothing to do with it.) Aug 17, 2014 at 16:21

3 Answers 3

1

There's a filter-hook that allows you to run some checking before comment is posted so you could use it too :

add_filter('preprocess_comment', 'wpse_158147_check_new_comment');
function wpse_158147_check_new_comment($commentdata){

    $commentdata['comment_content'] = preg_replace("/<tag(.*?)>(.*)<\/tag>/", "$2", $commentdata['comment_content']);// or str_replace
    return $commentdata;

}

Here "tag" would be stripped (to be replaced here with your specific tag).

0
0

Try hooking to a later hook for comment tags.

function my_comment_tags() {
    global $allowedtags;
    $allowedtags = array(
        'strong' => array(),
        'em' => array(),
        'a' => array(
          'href' => array (),
          'title' => array ())
    );
}

add_action('comment_post', 'my_comment_tags');
0

This code has them all removed. Simply add back what you want to return in the code which goes in your functions file

add_filter( 'comment_form_defaults', 'wpsites_remove_comment_form_allowed_tags' );
function wpsites_remove_comment_form_allowed_tags( $defaults ) {

    $defaults['comment_notes_after'] = '';
    return $defaults;

}
2
  • 2
    This code simply removes the message after the comment form, but the ability to write HTML in comments remains. Aug 17, 2014 at 16:07
  • What message? It removes the comment form allowed tags which display after the form. You can add back the ones you don't want to remove. Read your own question. You never mentioned removing HTML from the comment form. Aug 17, 2014 at 16:40

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