Site logo

Archived topic

Spam Prevention in Comments

7 replies · Started by Holley on August 5, 2019

Viewing posts 1–8 of 8

Hoping you can help me out with a little spam prevention. I've tried adding the following code to my GeneratePress child theme functions.php to disable the comments URL/website field and to disallow html in the comments section to try and cut down on spam comments but it doesn't seem to be working. I see in /structure/comments.php there is some customization going on with the comments section, maybe that's conflicting?

Disable URL field:

add_filter('comment_form_default_fields', 'unset_url_field');
function unset_url_field($fields){
if(isset($fields['url']))
unset($fields['url']);
return $fields;
}

Disallow html

function wpb_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 wpb_comment_display( $comment_to_display ) {
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
}
add_filter( 'preprocess_comment', 'wpb_comment_post', '', 1);
add_filter( 'comment_text', 'wpb_comment_display', '', 1);
add_filter( 'comment_text_rss', 'wpb_comment_display', '', 1);
add_filter( 'comment_excerpt', 'wpb_comment_display', '', 1);
remove_filter( 'comment_text', 'make_clickable', 9 );

Ah, perfect thanks.

How about for not allowing html in the comment field?

Just trying to cut down on bots posting links in the comments. I'll try that, thx!

No problem :)

Try AntiSpam Bee plugin instead. With Akismet, I usually see spam comments with URLs go through even with comment URL field disabled. But the plugin does not work with contact forms, so you might have to use google recaptcha with those. But it is vry effective and I have never received spam comments since I began using it.

Thanks for sharing!

This archived topic is closed to new replies.