Remove Links & Other Spam from Comments in WordPress

Some bloggers, link-building muddleheads may spam your comment area with multiple URLs. You can get rid of those by putting this code in your child theme’s functions.php file:

function gt_comment_post( $incoming_comment ) {
  $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
  $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
  return( $incoming_comment );

function gt_comment_display( $comment_to_display ) {
  $comment_to_display = str_replace( ''', "'", $comment_to_display );
  return $comment_to_display;

add_filter('preprocess_comment', 'gt_comment_post', ’, 1);
add_filter('comment_text', 'gt_comment_display', ’, 1);
add_filter('comment_text_rss', 'gt_comment_display', ’, 1);
add_filter('comment_excerpt', 'gt_comment_display', ’, 1);
Default image
Gaurav Tiwari
Gaurav Tiwari is a professional graphic & web designer from New Delhi, India. is his personal space where he writes on blogging, digital marketing, content writing, learning and business growth. Gaurav has contributed in developing more than 325 brands worldwide and while you are reading this, he's busy building a couple more.

Newsletter Updates

Get blogging, marketing and other growth-hacking tips in mailbox. Just enter your email here.