Archived topic
Spam Prevention in Comments
7 replies · Started by Holley on August 5, 2019
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 );
Hi there,
did you try the instructions provided here:
https://docs.generatepress.com/article/remove-e-mail-and-url-field-from-comment-form/
Ah, perfect thanks.
How about for not allowing html in the comment field?
Maybe something like this?
https://stackoverflow.com/questions/18168352/disabling-html-on-comments
If not you might need to check with WordPress.
HTML is generally allowed in comments as it doesn't really prevent spam or cause harm in your site.
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!