Home / comment / Remove HTML in comments
  • How to Remove HTML in comments

    no thumbnail sorry: Remove HTML in comments

    Adding this code to the functions.php file of your wordpress theme will remove HTML from your user comments.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    // This will occur when the comment is posted
    function plc_comment_post( $incoming_comment ) {
    	// convert everything in a comment to display literally
    	$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
    	// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
    	$incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );
    	return( $incoming_comment );
    }
    // This will occur before a comment is displayed
    function plc_comment_display( $comment_to_display ) {
    	// Put the single quotes back in
    	$comment_to_display = str_replace( '&apos;', "'", $comment_to_display );
    	return $comment_to_display;
    }
    add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
    add_filter( 'comment_text', 'plc_comment_display', '', 1);
    add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
    add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);
    

    ( WordPress codex functions, hooks, in this snippet. )

    comment_excerpt, comment_text, comment_text_rss, preprocess_comment, comment_post, add_filter, _e, _ex,