Send Comments With Links in the Name Field to Spam
Marks comments as spam when the name field contains a web address, a classic trick of spam bots.
Opens WPIDE on
Code
add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) {
if ( is_wp_error( $approved ) || ! empty( $commentdata['user_id'] ) ) {
return $approved;
}
$name = isset( $commentdata['comment_author'] ) ? (string) $commentdata['comment_author'] : '';
return preg_match( '#https?://|www\.|\.(com|net|ru|xyz|top)\b#i', $name ) ? 'spam' : $approved;
}, 20, 2 );
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.