Are you looking for a way to remove HTML from your user comments? While there’s probably a plugin for this, we have created a quick code snippet that you can use to remove HTML in comments in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
// 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( "'", ''', $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( ''', "'", $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);
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 23 best WordPress themes for non-profits and how to track affiliate links in Google Analytics.
Comments Leave a Reply