Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
<pre> isn't allowed by default. I think this setting is getting changed elsewhere. Maybe by a plugin. –  sakibmoon Aug 17 '14 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.) –  Yoav Kadosh Aug 17 '14 at 16:21

3 Answers 3

up vote 1 down vote accepted

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).

share|improve this answer
    
This is the only working solution. Thank you! –  Yoav Kadosh Aug 17 '14 at 16:48

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');
share|improve this answer

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;

}
share|improve this answer
    
This code simply removes the message after the comment form, but the ability to write HTML in comments remains. –  Yoav Kadosh Aug 17 '14 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. –  Brad Dalton Aug 17 '14 at 16:40

protected by Community Dec 21 '14 at 5:42

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

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.